Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm starting the study of Python/C API and I make the first code to test some functions, I write this:

file: test.c

#include "Python.h"

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

I compile with command:

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

I've the error undefined reference to `Py_BuildValue'

After I run:

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

this compile without errors, but when I run the compiled file I've a

Segmentation fault (core dumped)

How do I set the gcc parameters?

I've ubuntu 12.04, python 2.7.3, gcc 4.6.3 and I installed python-dev.

Thanks.

share|improve this question
    
What are your linker flags? –  StoryTeller Oct 18 '12 at 12:50
    
You probably don't wait to have a main, and you may want to build a shared object with e.g. gcc -Wall -fPIC -shared -I/usr/include/python2.7 test.c -o test.so then do Python tricks to dlopen that ./test.so –  Basile Starynkevitch Oct 18 '12 at 12:51
1  
You should compile it with: gcc -I/usr/include/python2.7 test.c -lpython2.6 (or whatever version you're running). –  Pablo Oct 18 '12 at 12:52
    
The BasileStarynkevitch's suggestion raise Segmentation fault, instead @Pablo's suggestion run! Thanks! :D –  Figus Oct 18 '12 at 13:05
    
Aren't you supposed to have a pyInit kinda expression for embedding python interpreter? –  G Sree Teja Simha Oct 18 '12 at 13:14

1 Answer 1

In the comments @Pablo has provided the solution

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

I forgot to link the python library with the "-l" parameter.

-llibrary -l library Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX) compliance and is not recommended.)It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o' searches libraryz' after file foo.o but before bar.o. If bar.o refers to functions in 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' and `.a' and searches several directories. 

Parameter description source

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.