【问题标题】:First example does not compile due to compiler errors由于编译器错误,第一个示例无法编译
【发布时间】:2014-03-05 14:11:25
【问题描述】:

由于编译器错误,我在为 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-path,但错误仍然存​​在。

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 中的编译器设置有关,但我无法让它工作。

提前致谢

【问题讨论】:

  • 那些不是编译器错误,而是链接器错误。这意味着它找不到您(直接或间接)调用的某些函数。要解决它,您需要链接到包含这些文件的库。
  • 来自 SWIG wiki 的 This entry 看起来可能是相关的。

标签: c gcc compiler-errors linker swig


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 感谢您的提示,但错误仍然存​​在。错误消息不会改变。我不知道继续,我无法摆脱这个错误,即使我现在已经阅读了很多关于这个错误的内容。我怀疑,这与 cygwin 下 gcc 的使用有关,但我不确定。我对他们俩的经验都太少了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多