gpt4 book ai didi

linker - undefined reference - 尽管链接器找到了 lib

转载 作者:行者123 更新时间:2023-12-04 01:28:09 38 4
gpt4 key购买 nike

我有一个简单的程序来测试 python 开发文件的可用性:

#include<Python.h>
int main(){Py_Initialize(); Py_Finalize(); }

我将它(安装了 python 2.7)编译为 gcc -I/usr/include/python2.7 -lpython2.7 p.c .它在其他机器上运行良好,除了在 Ubuntu 12.04(精确)的大部分清洁 chroot 上我不断得到
/tmp/ccj8Mgjb.o: In function `main':
p.c:(.text+0x5): undefined reference to `Py_Initialize'
p.c:(.text+0xa): undefined reference to `Py_Finalize'
collect2: ld returned 1 exit status

安装了 header , /usr/lib/libpython2.7.so存在但链接器仍然失败。该符号列在 .so 中文件,并且 gcc 正在读取正确的 libpython2.7.so :
$ nm -D libpython2.7.so.1.0  | grep Py_Initialize
00000000000c9c20 T Py_Initialize
00000000000c9260 T Py_InitializeEx

$ strace -f gcc -I/usr/include/python2.7 -lpython2.7 /tmp/p.c 2>&1 |grep libpython2.7 |grep open
[pid 10618] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/libpython2.7.so", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 10618] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/libpython2.7.a", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 10618] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libpython2.7.so", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 10618] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libpython2.7.a", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 10618] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libpython2.7.so", O_RDONLY) = 7

有任何想法吗?

最佳答案

尝试:

gcc -I/usr/include/python2.7 p.c -lpython2.7 

链接器还不知道 Py_Initialize是加载时必需的符号 libpython2.7.a ,所以它把它扔掉。然后它进入 p.o 并对丢失的符号提出异议。以这种方式排序将使链接器在后续输入中查找丢失的符号。

见: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o' searches libraryz' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.

关于linker - undefined reference - 尽管链接器找到了 lib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13951166/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com