【问题标题】:Bad reloc address 0x0 in section.data C extensions for pythonpython的section.data C扩展中的错误reloc地址0x0
【发布时间】:2014-12-09 21:52:06
【问题描述】:

我正在尝试编写一个脚本来在 python 中自动化设备。该设备是用 C 语言编程的,我目前正在尝试编写一个 C 包装器,以便稍后从 Python 调用这些函数。我正在关注this 教程。

原始的 C 函数隐藏在 .lib 文件中,但提供了包含所有函数初始化的头文件。这是它的外观的sn-p

#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);

这是我正在尝试创建的 C 包装器

#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);
}

}

我正在尝试的编译调用是

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

根据我的搜索,我在 SO 上找到了 this 问题并尝试了这个 gcc -shared -IC:/Python27/include -LC:/Python27/libs myModule.c -DVNX_ATTEN_EXPORTS=1 -lpython27 -o myModule.pyd

它没有帮助。

我收到错误“.data 部分中的错误重定位地址 0x0”“collect2.exe:错误:ld 返回 1 退出状态”

在 Windows XP(32 位)平台上使用 MinGW 库和 Python 2.7 进行编译。

如果您需要任何进一步的信息,请告诉我,并提前致谢!! PS:这算不算交叉编译?

编辑:添加了整个错误消息

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

【问题讨论】:

  • 在最后一条错误消息上方显示行
  • 尝试在 #include"VNX_atten.h"(基于随软件提供的示例 C 程序)和包装器(基于链接 here 的另一个 SO 答案)。还是一样的错误。
  • 使用 Extern C 编辑代码,使我可以从 python 调用函数而不会出错。

标签: c++ python-2.7 mingw wrapper cross-compiling


【解决方案1】:

您必须使用 g++ ... -llibraryName 链接 lib 文件。

【讨论】:

  • 天哪,真是愚蠢!万分感谢!。我没有声望投票赞成你的答案。但我会将其标记为已接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 2016-11-17
  • 2022-08-18
相关资源
最近更新 更多