2013-05-26 20 views
5

ho questo codice C:undefined reference to `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] ... 

che dà questo errore durante la compilazione:

/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 è inclusa nella parte superiore del file di origine.

Sto compilando con:

gcc -funwind-tables -rdynamic -I /usr/include/python2.7/ -g -o Scribe Scribe.c -lcurses `python-config --cflags` `python-config --ldflags` -Wall 

sto basando il codice sui documenti Python C-API, da qui:

http://docs.python.org/2/c-api/

In particolare:

http://docs.python.org/2/c-api/string.html?highlight=pystring_fromstring#PyString_FromString

Non ho idea del motivo per cui questo è accad pening, halp? = c

+0

Altri riferimenti alle funzioni Python C-Api hanno questo problema o è solo questo per 'PyString_FromString'? – martineau

+0

@martineau È solo PyString_FromString, con quella linea commentata compila bene. – Alex

+0

Sembra un linker - non compilatore - errore. Non hai bisogno di un'opzione '-L/path/to/libs' in modo che possa trovare il codice oggetto per le funzioni Python C-Api? – martineau

risposta

4

Risolto grazie all'aiuto di martineau.

Risulta il flag python-config --cflags e python-config --ldflags generato da linee che includeva la directory python3.3 include nel percorso di ricerca e collegava il lib python3.3.

Naturalmente python3.3 non funziona molto bene con python2.7 C-API, che è ciò che ha causato questo problema.

La mia soluzione era quella di copiare l'output di python-config --cflags e python-config --ldflags e modificarlo in modo che comprendeva python2.7 invece di 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 

Invece di:

-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 
+0

Felice di vederti risolto il tuo problema. – martineau

+0

Hmm, questa non sembra essere la fonte del mio problema, dal momento che le mie bandiere sembrano già a posto. –

1

L'ordine di le librerie sono importanti Prova a compilare con -lpython2.7 che appare per ultimo nella lista delle librerie.

Problemi correlati