小编joh*_*p99的帖子

Python - 编译期间未解析的Py_Initialize

我已经静态编译了Python2.7而没有任何错误.要测试我的构建,我使用以下代码段:

#include "Python.h"
int main()
{
   Py_Initialize();
}
Run Code Online (Sandbox Code Playgroud)

我正在编译它:

$ gcc -static -I/path/to/python/header -L/path/to/my/staticpythonlib \ 
 -lpython2.7 -ldl -l_all_other_needed_lib /tmp/my_previous_snippet.c -o myouput
Run Code Online (Sandbox Code Playgroud)

但是,发生了错误.gcc声称着名undefined reference.

test.c :(.text + 0x1):对'Py_Initialize'的未定义引用

奇怪的是我使用带有详细标记的gcc(我不会在这里粘贴结果)并且编译器说,它使用的是我的libpython,但找不到引用.所以我列出了我的静态python2.7库的符号:

$ nm /path/to/pythonlib |grep Py_Initialize
frozenmain.o           U Py_Initialize
pythonrun.o  0000009e9 T Py_Initialize
pythonrun.o  000000052 T Py_Initialize_Ex
main.o                 U Py_Initialize
Run Code Online (Sandbox Code Playgroud)

我们可以看到,Py_Initializepythonrun.o中正确引用了它.但是我不知道编译器如何选择正确的目标文件.

我的问题是:

  1. 我怎么能确定gcc在我的.a lib中使用了正确的目标文件?
  2. 我的编译选项有什么问题吗?

谢谢你的帮助.

python gcc static-libraries unresolved-external

6
推荐指数
1
解决办法
5763
查看次数