4

我开始研究 Python/C API,并编写了第一个代码来测试一些函数,我写了这个:

文件:test.c

#include "Python.h"

int main() {
    PyObject* none = Py_BuildValue("");
}

我用命令编译:

gcc -I/usr/include/python2.7 test.c

我有错误未定义对“Py_BuildValue”的引用

我运行后:

gcc -I/usr/include/python2.7 --shared -fPIC hashmem.c

这个编译没有错误,但是当我运行编译文件时,我有一个

Segmentation fault (core dumped)

如何设置 gcc 参数?

我有 ubuntu 12.04、python 2.7.3、gcc 4.6.3 并安装了 python-dev。

谢谢。

4

1 回答 1

2

在评论中@Pablo 提供了解决方案

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

我忘了用“-l”参数链接python库。

-llibrary -l library 链接时搜索名为 library 的库。(将库作为单独参数的第二种选择仅适用于 POSIX)合规性,不推荐使用。)在命令中编写此选项的位置有所不同;链接器按照指定的顺序搜索和处理库和目标文件。因此,foo.o -lz bar.o' searches libraryz' 在文件 foo.o 之后但在 bar.o 之前。如果 bar.o 引用z', those functions may not be loaded.The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.The directories searched include several standard system directories plus any that you specify with -L.Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that - l surrounds library withlib' 和 `.a' 中的函数并搜索多个目录。 

参数说明来源

于 2012-10-18T15:43:07.847 回答