By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

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

P: n/a
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@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@localhost Opgave03]$

I searched the newsgroup and found this thread:
http://groups.google.com/group/comp....b836f369bd0042

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.
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
sp********@gmail.com schrieb:
Can anyone help? I am including Python.h, so why does it not find
Py_BuildValue?

Read your error message again. It says the dynamic linker (ld) can't
find the name. You have to link against the Python library.

Christian

Jun 27 '08 #2

P: n/a
On Wed, 04 Jun 2008 05:57:20 -0700, spectrumdt wrote:
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@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@localhost Opgave03]$

I searched the newsgroup and found this thread:
http://groups.google.com/group/comp....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.
Hi! Your C code contains too many errors. I'm lazy to comment them all.

Here's my take on the trivial "Hello world" in Python C API:

1. create 'hello.c' file with the following content:
#include <Python.h>

PyObject*
hello(PyObject* self)
{
printf("Hello world!\n");
Py_RETURN_NONE;
}

static PyMethodDef functions[] = {
{"hello", (PyCFunction)hello, METH_NOARGS},
{NULL, NULL, 0, NULL},
};

DL_EXPORT(void)
init_hello(void)
{
Py_InitModule("_hello", functions);
}

2. create 'buildme.py' file with this content:
import os
import sys
from distutils.core import Extension, setup

os.chdir(os.path.dirname(os.path.abspath(__file__) ))
sys.argv = [sys.argv[0], 'build_ext', '-i']
setup(ext_modules = [Extension('_hello', ["hello.c"])])

3. run "python buildme.py"

That's all.
>>from _hello import hello
hello()
Hello world!
-- Ivan
Jun 27 '08 #3

P: n/a
On Jun 4, 4:13*pm, Ivan Illarionov <ivan.illario...@gmail.comwrote:
Hi! Your C code contains too many errors. I'm lazy to comment them all.

2. create 'buildme.py' file with this content:
Thanks for the replies.

Maybe I should have read the rest of the guide to extending Python
with C before whining here. I hadn't noticed the part where I was
supposed to create a script to compile my C code rather than just call
gcc. I fixed that, and now it works. Thanks.

Yeah, my C code is probably full of bugs, too. I am a sucky C
programmer. :P
Jun 27 '08 #4

P: n/a
sp********@gmail.com wrote:
I am trying to extend Python with some C code.
Have you considered using Cython instead of C?

http://cython.org/

Stefan
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.