0

我是 C 菜鸟,所以这可能是一个愚蠢的问题。我正在尝试编译 C 对象(.o 文件)的 .so 文件(共享库,如果我的术语正确的话),目的是通过 .so 将它们导入 Python ctypes。我首先编译了 *.so

gcc -shared -o libvARAM.so ARAM.o ARAM_io.o io.o pre.o rule.o stat.o ART.o vARAM.o

这行得通,除了当我尝试通过 ctypes 加载时,我得到了奖励:

OSError: ./libvARAM.so: undefined symbol: max

经过一番挖掘,我意识到 max 不是标准的 C 函数。打电话ldd libvARAM.so告诉我依赖项之一是libc.so.6. 我创建了一个符号链接libc.solibc.so.6然后尝试重新编译我的 .so 为

gcc -shared -o libvARAM.so ARAM.o ARAM_io.o io.o pre.o rule.o stat.o ART.o vARAM.o -llibc

这产生了

/usr/bin/ld: cannot find -llibc

如果我也尝试会产生同样的错误-L/lib/i386-linux-gnu/ -llibc。我知道这个线程,但觉得它与我的情况无关,因为解决方案是生成文件。如果这很重要,我正在使用 Xubuntu。

真诚感谢任何帮助!

4

2 回答 2

2

一:链接器标志不能那样工作。对于libXYZ.so,相应的链接器标志是 NOT-llibXYZ但只是-lXYZ

二:即使这样也不需要,因为 C 标准库 ( -lc) 会自动链接到可执行文件。

Three: your problem most likely is that there should be a max() macro (as opposed to a function) defined in one of the header files, but you don't include this header file, so the compiler doesn't know it's a macro and treats it as a function - thenof course it can't find it in libc.so because it's not there.

于 2012-09-01T17:00:50.093 回答
0

Posting this so that if other newbies see it, they'll see the answer... Basically, my header file was as follows:

#ifdef UNIX
#define min(x, y) (x<y ? x : y)
#define max(x, y) (x<y ? y : x)
#endif

Removing the first and last lines (i.e. no ifdef check) allowed the compiler to read these definitions. Apparently it had not been reading them.

于 2012-09-02T19:56:16.333 回答