4

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.

5
  • 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 Commented Oct 18, 2012 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). Commented Oct 18, 2012 at 12:52
  • The BasileStarynkevitch's suggestion raise Segmentation fault, instead @Pablo's suggestion run! Thanks! :D Commented Oct 18, 2012 at 13:05
  • Aren't you supposed to have a pyInit kinda expression for embedding python interpreter? Commented Oct 18, 2012 at 13:14
  • @G Sree Tema Si ha In this test I want only test Python/C functions band note create a python module Commented Oct 18, 2012 at 13:33

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.