Vikram Raghu
Vikram Raghu

Reputation: 53

Bad reloc address 0x0 in section.data C extensions for python

I'm trying to write a script to automate a device in python. The device is programmed in C and I'm currently attempting to write a C wrapper in order for me to call those functions from Python later. I'm following this tutorial.

The original C functions are hidden in a .lib file but the header file with all the functions initialization is provided. Here is a snippet of what it looks like

#ifdef VNX_ATTEN_EXPORTS
#define VNX_ATTEN_API __declspec(dllexport)
#else
#define VNX_ATTEN_API __declspec(dllimport)
#endif


VNX_ATTEN_API void fnLDA_SetTestMode(bool testmode);
VNX_ATTEN_API int fnLDA_GetNumDevices();
VNX_ATTEN_API int fnLDA_GetDevInfo(DEVID *ActiveDevices);
VNX_ATTEN_API int fnLDA_GetModelName(DEVID deviceID, char *ModelName);
VNX_ATTEN_API int fnLDA_InitDevice(DEVID deviceID);
VNX_ATTEN_API int fnLDA_CloseDevice(DEVID deviceID);
VNX_ATTEN_API int fnLDA_GetSerialNumber(DEVID deviceID);
VNX_ATTEN_API int fnLDA_GetDeviceStatus(DEVID deviceID);

Here is the C wrapper that I'm attempting to create

#include <stdio.h>
#include <Python.h>
extern "C" {
#include "VNX_atten.h"
}
//#include <stdafx.h>

/*
 * Function to be called from Python
 */

extern "C" { 
static PyObject* py_fnLDA_SetTestMode(PyObject* self, PyObject* args)
{
    double x;
    double y = 1;
    PyArg_ParseTuple(args, "d", &x);
    if(x==1)
    fnLDA_SetTestMode(true);
    else
    fnLDA_SetTestMode(false);

    return Py_BuildValue("d", y);
}

/*
 * Another function to be called from Python
 */
static PyObject* py_myOtherFunction(PyObject* self, PyObject* args)
{
    double x, y;
    PyArg_ParseTuple(args, "dd", &x, &y);
    return Py_BuildValue("d", x*y);
}

/*
 * Bind Python function names to our C functions
 */
static PyMethodDef myModule_methods[] = {
    {"fnLDA_SetTestMode", py_fnLDA_SetTestMode, METH_VARARGS},
    {"myOtherFunction", py_myOtherFunction, METH_VARARGS},
    {NULL, NULL}
};

/*
 * Python calls this to let us initialize our module
 */
void initmyModule()
{
    (void) Py_InitModule("myModule", myModule_methods);
}

}

The compilation call I'm trying is

g++ -shared -IC:/Python27/include -LC:/Python27/libs myModule.cpp -lpython27 -o myModule.pyd

Based on my searches I found this question on SO and tried this gcc -shared -IC:/Python27/include -LC:/Python27/libs myModule.c -DVNX_ATTEN_EXPORTS=1 -lpython27 -o myModule.pyd

It hasn't helped.

I'm getting the error "bad reloc address 0x0 in section .data" "collect2.exe: error: ld returned 1 exit status"

Compilation is on a Windows XP (so 32 bit) platform using MinGW libraries and Python 2.7.

Let me know if you need any further information and thanks in advance!! PS: Does this count as cross compiling?

EDIT: ADDED THE ENTIRE ERROR MESSAGE

C:\cygwin\home\VRaghu\Attenuator\LDA SDK\ANSI C SDK\VNX_Atest>g++ -shared -IC:/P
ython27/include -LC:/Python27/libs myModule.cpp -DVNX_ATTEN_EXPORTS=1 -lpython27
 -o myModule.pyd

C:\DOCUME~1\VRaghu\LOCALS~1\Temp\ccFkPRnf.o:myModule.cpp:(.text+0x40): undefined
 reference to `fnLDA_SetTestMode'

C:\DOCUME~1\VRaghu\LOCALS~1\Temp\ccFkPRnf.o:myModule.cpp:(.text+0x50): undefined
 reference to `fnLDA_SetTestMode'

c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\DOCUME~
1\VRaghu\LOCALS~1\Temp\ccFkPRnf.o: bad reloc address 0x0 in section `.data'
collect2.exe: error: ld returned 1 exit status

Upvotes: 3

Views: 5885

Answers (1)

erenon
erenon

Reputation: 19128

You have to link the lib file using g++ ... -llibraryName.

Upvotes: 9

Related Questions