I have this C code:
... [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] ...
Which gives this error when compiled:
/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 is included at the top of the source file.
I'm compiling with:
gcc -funwind-tables -rdynamic -I /usr/include/python2.7/ -g -o Scribe Scribe.c -lcurses `python-config --cflags` `python-config --ldflags` -Wall
I'm basing the code on the Python C-Api docs, from here:
http://docs.python.org/2/c-api/
Specifically:
http://docs.python.org/2/c-api/string.html?highlight=pystring_fromstring#PyString_FromString
I have no idea why this is happening, halp? =c
Solved it, thanks to some help from martineau.
Turns out the python-config --cflags
and python-config --ldflags
lines generated flags that included the python3.3 include directory in the search path and linked the python3.3 lib.
Naturally python3.3 doesn't work so well with python2.7 C-API, which is what caused this problem.
My solution was to copy the output of python-config --cflags
and python-config --ldflags
and edit it so it included python2.7 instead of python3.3m:
-I/usr/include/python2.7 -I/usr/include/python2.7 -Wno-unused-result -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2
-lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic
Instead of:
-I/usr/include/python3.3m -I/usr/include/python3.3m -Wno-unused-result -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2
-lpthread -ldl -lutil -lm -lpython3.3m -Xlinker -export-dynamic
The order of the libraries is important. Try compiling with -lpython2.7 appearing last in the library list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With