Python - Py_Initialize在编译期间未解决 [英] Python - Py_Initialize unresolved during compilation
问题描述
我有静态编译的Python2.7没有任何错误。
为了测试我的构建,我使用了以下代码片段:
#includePython.h
int main()
{
Py_Initialize();
}
我正在编译它:
$ 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
然而,发生错误。 gcc声称着名的未定义的引用
。
$ b
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
我们可以看到,pythonrun.o中正确引用了 Py_Initialize
。但是我不知道编译器如何选择正确的目标文件。
我的问题是:
$ ol b $ b
感谢您的帮助。
问题!更具体地说,gcc的参数顺序很重要。更具体地说,如果 bar
对象使用库 bleh $ c中的函数
bluh
$ c>,那么订单 -lbleh bar.o
有问题,因为它没有理由让gcc寻找函数 bluh
在 bleh
中。另一方面,当你使用 bar.o -lbleh
时,gcc知道你指的是 bluh
,当它得到处理 -lbleh
它试图解决依赖性。这在 http://gcc.gnu.org/onlinedocs/gcc/链路Options.html 。通常,在对象之后指定库。
为了重现您的问题,请创建一个文件 a1.c
如下所示:
#includePython.h
int main()
{
Py_Initialize();
返回0;
}
现在用 gcc -static -I / usr /include/python2.7 -L / usr / lib / python2.7 / config / -lpython2.7 -ldl -lm -lutil -lz -pthread -o a1 a1.c
。这给出了对 Py_Initialize
的未定义引用。当然,你必须改变路径来匹配你的安装。
现在,用 gcc -static -I / usr / include / python2.7 -L / usr / lib / python2.7 / config / -o a1 a1.c -lpython2.7 -ldl -lm -lutil -lz -pthread
并且它起作用(忽略可能的许多警告,这是一个不同的问题)。
I have statically compiled Python2.7 without any error. To test my build, I use the following snippet:
#include "Python.h"
int main()
{
Py_Initialize();
}
And I am compiling it like this:
$ 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
However, an error occured. gcc claims the famous undefined reference
.
test.c:(.text+0x1): Undefined reference to 'Py_Initialize'
Curiously I used gcc with the verbosity flag (I won't paste the result here) and the compiler says, it's using my libpython, but couldn't find the reference. So I listed the symbols of my static python2.7 library :
$ 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
We can see, that Py_Initialize
is correctly referenced in pythonrun.o. However i don't know how the compiler chose the correct object file.
My questions are :
- How can I be sure, that gcc uses the correct object file in my .a lib?
- Is there anything wrong on my compilation options?
Thanks for your help.
The order matters! More specifically, the order in the arguments for gcc matters. More specifically, if a bar
object uses a function bluh
from library bleh
, then the order -lbleh bar.o
is problematic because it provides no reason to gcc look for the function bluh
in bleh
. On the other hand, when you use bar.o -lbleh
then gcc knows you are referring to bluh
and when it gets to handle -lbleh
it tries to resolve the dependence. This is quickly mentioned at http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html. As a rule, always specifies libraries after your objects.
To reproduce your problem, create a file a1.c
as in:
#include "Python.h"
int main()
{
Py_Initialize();
return 0;
}
Now compile with gcc -static -I/usr/include/python2.7 -L/usr/lib/python2.7/config/ -lpython2.7 -ldl -lm -lutil -lz -pthread -o a1 a1.c
. This gives the undefined reference to Py_Initialize
. Of course you have to change the paths to match your installation.
Now, instead, compile with gcc -static -I/usr/include/python2.7 -L/usr/lib/python2.7/config/ -o a1 a1.c -lpython2.7 -ldl -lm -lutil -lz -pthread
and it works (ignoring the possible many warnings, which is a different matter).
这篇关于Python - Py_Initialize在编译期间未解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!