Trying to extend Python with C: undefined reference to `Py_BuildValue'

spectrumdt at gmail.com spectrumdt at gmail.com
Wed Jun 4 08:57:20 EDT 2008


  Hello.

  I am trying to extend Python with some C code. I made a trivial
"Hello World" program in C that I am trying to wrap in "boilerplate"
for inclusion in a Python program. But I can't compile the C code. The
C compiler cannot find the required function `Py_BuildValue'.

  My C code looks like this:



  #include "/usr/include/python2.5/Python.h"

  /* Python wrapper for 'main'. */
  static PyObject* mpi_main() {
    int res = main();
    PyObject* retval = (PyObject*)Py_BuildValue("i",res);
  }

  /* Main. A 'Hello World' function. */
  int main() {
    printf ("Hello, World. I am a C function.\n");
  }



  And my error message looks like this:



  [ore at localhost Opgave03]$ gcc ctest.c
  /tmp/ccH46bs8.o: In function `mpi_main':
  ctest.c:(.text+0x1d): undefined reference to `Py_BuildValue'
  collect2: ld returned 1 exit status
  [ore at localhost Opgave03]$



  I searched the newsgroup and found this thread:

 
http://groups.google.com/group/comp.lang.python/browse_thread/thread/c6d70e02a6bc9528/87b836f369bd0042?lnk=gst&q=undefined+reference+to+%60Py_BuildValue%27#87b836f369bd0042

  It recommended that I add the option "-Wl,--export-dynamic" when
calling gcc. That makes no difference. The thread also has some more
stuff that I don't quite understand.

  Can anyone help? I am including Python.h, so why does it not find
Py_BuildValue?

  Thanks in advance.



More information about the Python-list mailing list