我有這樣的C代碼:未定義參考`PyString_FromString」
... [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
我不知道爲什麼這是hap pening,halp? = c
其他對Python C-Api函數的引用是否存在這個問題呢?還是隻是這個用於'PyString_FromString'? – martineau
@martineau它只是PyString_FromString,該行註釋掉它編譯得很好。 – Alex
它看起來像一個鏈接器 - 不是編譯器 - 錯誤。你不需要一個'-L/path/to/libs'選項,它可以找到Python C-Api函數的目標代碼嗎? – martineau