[英]Python - Py_Initialize unresolved during compilation
I have statically compiled Python2.7 without any error. 我已经静态编译了Python2.7而没有任何错误。 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
. gcc声称着名的undefined reference
。
test.c:(.text+0x1): Undefined reference to 'Py_Initialize' test.c :(。text + 0x1):对'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. 奇怪的是我使用带有详细标记的gcc(我不会在这里粘贴结果)并且编译器说,它使用的是我的libpython,但找不到引用。 So I listed the symbols of my static python2.7 library : 所以我列出了我的静态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
We can see, that Py_Initialize
is correctly referenced in pythonrun.o. 我们可以看到, Py_Initialize
在pythonrun.o中被正确引用。 However i don't know how the compiler chose the correct object file. 但是我不知道编译器如何选择正确的目标文件。
My questions are : 我的问题是:
Thanks for your help. 谢谢你的帮助。
The order matters! 订单很重要! More specifically, the order in the arguments for gcc matters. 更具体地说,gcc的参数中的顺序很重要。 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
. 更具体地说,如果一个bar
对象使用了一个函数bluh
from library bleh
,那么order -lbleh bar.o
是有问题的,因为它没有理由让gcc在bluh
中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. 另一方面,当你使用bar.o -lbleh
gcc知道你指的是bluh
,当它处理-lbleh
它试图解决依赖。 This is quickly mentioned at http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html . 这在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: 要重现您的问题,请创建一个文件a1.c
如下所示:
#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
. 现在使用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
. 这给出了对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). 现在,改为使用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 am using python 2.7 and set the path of python installation directory in the environment variable. 我正在使用python 2.7并在环境变量中设置python安装目录的路径。 I also use boost 1.5.0 我也使用boost 1.5.0
follow the steps to Compile the below code 按照以下步骤编译以下代码
#include <python2.7/Python.h>
int main()
{
Py_Initialize();
PyRun_SimpleString("print \"Hello, world!\"");
Py_Finalize();
return 0;
}
Run the command:- 运行命令: -
g++ hello.cc -lpython2.7
./a.out
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.