473,287 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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

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
4 5359
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: terti | last post by:
Hi, // I need some help to embed the equivalent of the following Python code in a C DLL. from NLRclient import NLRclient i = NLRclient("server") s = i.getStrSort("qwerty") // This what I...
2
by: Torsten Mohr | last post by:
Hi, when i write an extension module in C and return a Py_Object* that i've built with Py_BuildValue, do i need to use Py_INCREF on that before i return it to python from my extension module or...
2
by: ashtonn | last post by:
Hello, How do i print values returned by Py_BuildValue in Linux? PyObject *obj = Py_BuildValue("{s:i}", "Status", status); I need to print the Status value here -Thanks, Ashton
3
by: ch424 | last post by:
Hi there, I'm using Python 2.4.1 on Ubuntu Linux, and I'm having problems extending python in C: The C code is below: #include <Python.h> #include "ni488.h"
2
by: Simon Newton | last post by:
Hi, I've just starting out embedding Python in C and get the following error when I try and import a module that in turn imports the math module. ImportError:...
7
by: wardm | last post by:
I have created a Dict object in a C++ App that calls (embedded) Python functions. The Dict is to be used to pass variable data between the C++ App and the python functions. However I cannot get...
2
by: Martin Kulas | last post by:
Hallo! I have a problem with Py_BuildValue: I want to convert an unsigned int to a PyObject *. http://docs.python.org/api/arg-parsing.html says that I can use "I" as a format string. But it...
3
by: Christian Meesters | last post by:
Hi, currently I have a problem understanding Py_BuildValue. I have this code: static PyObject *function(PyObject *self, PyObject *args) { PyObject * python_return_value = NULL; PyObject *...
0
by: Tim Spens | last post by:
The following is a simple complete example using the c python api to generate callbacks from c to python. But when I run the c code I get a segfault in PyInt_FromLong () (see below). Most of this...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.