我有这个 C 代码:
... [SNIP] ...
for(Node = Plugin.Head; Node != NULL; Node = Node->Next) {
//Create new python sub-interpreter
Node->Interpreter = Py_NewInterpreter();
if(Node->Interpreter == NULL) {
Die("Py_NewInterpreter() failed");
}
//Create path to plugins main source file
snprintf(Filename, FILENAME_MAX, "%s/main.py", Node->File);
//Convert filename to python string
PFilename = PyString_FromString(Filename);
if(PFilename == NULL) {
Die("PyString_FromString(%s) failed", Filename);
}
//Import plugin main source file
PModule = PyImport_Import(PFilename);
if(PModule == NULL) {
Die("PyImport_Import(%s) failed", Filename);
}
//Deallocate filename
Py_DECREF(PFilename);
//Get reference to onLoad function from module
PFunction = PyObject_GetAttrString(PModule, "onLoad");
if(PFunction == NULL) {
Die("PyObject_GetAttrString() failed");
}
}
... [SNIP] ...
编译时出现此错误:
/tmp/ccXNmyPy.o: In function `LoadPlugins':
/home/alex/Code/Scribe/Scribe.c:693: undefined reference to `PyString_FromString'
collect2: error: ld returned 1 exit status
Python.h 包含在源文件的顶部。
我正在编译:
gcc -funwind-tables -rdynamic -I /usr/include/python2.7/ -g -o Scribe Scribe.c -lcurses `python-config --cflags` `python-config --ldflags` -Wall
我将代码基于 Python C-Api 文档,来自这里:
http://docs.python.org/2/c-api/
具体来说:
http://docs.python.org/2/c-api/string.html?highlight=pystring_fromstring#PyString_FromString
我不知道为什么会这样,halp?=c