c - 第一个示例由于编译器错误而无法编译

标签 c gcc compiler-errors linker swig

由于编译器错误,我在为 python 包装器编译 swig 的第一个教程示例时遇到问题。

我引用的是这个教程http://www.swig.org/Doc1.3/Python.html#Python_nn4

我创建了所有文件并执行了“31.2.3 手动编译动态模块”中指定的命令。

背景:我在windows 64位系统上的cygwin中使用swig和gcc。

swig -python example.i
gcc -O2 -fPIC -c example.c
gcc -O2 -fPIC -c example_wrap.c -I/usr/include/python2.7/

这些命令工作正常。 在最后一步,出现了很多错误。我玩了至少2个小时,没发现错误。我也包含了所有相关的 python 路径,但错误仍然存​​在。

gcc -shared example.o example_wrap.o -o _example.so
example_wrap.o:example_wrap.c:(.text+0x4e): undefined reference to `PyArg_ParseTuple'
example_wrap.o:example_wrap.c:(.text+0x4e): relocation truncated to fit: R_X86_64_PC32      against undefined symbol `PyArg_ParseTuple'
example_wrap.o:example_wrap.c:(.text+0x79): undefined reference to `PyLong_AsLong'
example_wrap.o:example_wrap.c:(.text+0x79): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `PyLong_AsLong'
...

有人知道问题出在哪里吗?我想这与我在 cygwin 中的 gcc 中的编译器设置有关,但我无法让它工作。

提前致谢

最佳答案

您需要链接到语言库,在您的情况下是 Python。这就是定义这些符号的地方。你将需要类似的东西

gcc -shared -L/path/to/python/lib/folder -lpython2.7 ...

如果错误仍然存​​在,请尝试

gcc -v -shared -L/path/to/python/lib/folder -lpython2.7 ...

并查看结果输出中是否有任何信息表明您可能做错了什么(错误的Python库名称等)。如果仍然卡住,请将输出复制到您的问题并评论您的更新,以便我收到通知。

关于c - 第一个示例由于编译器错误而无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22200174/

相关文章:

使用 Arduino IDE 创建 I2C 光传感器的回调函数

c - 在 C 中将文件 Strtok 放入数组中

gcc - 帮助链接器失败 : . gnu.linkonce.t

c++ - 如何从.cpp文件和makefile编译程序

c - 关闭套接字时选择出现问题

c - Malloc 不断返回相同的指针

编译器不识别 __STATIC_INLINE

c++ - 显式特化中不允许存储类

c++ - '登录': identifier not found

visual-studio-2013 - 找不到元数据文件.dll CSC错误