473,804 Members | 3,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to extend Python with C: undefined reference to`Py_BuildValu e'

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 "boilerplat e"
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_B uildValue("i",r es);
}

/* 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 5429
sp********@gmai l.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 "boilerplat e" 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_B uildValue("i",r es);
}

/* 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/
c6d70e02a6bc952 8/87b836f369bd004 2?lnk=gst&q=und efined+referenc e+to+%
60Py_BuildValue %27#87b836f369b d0042
>
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)he llo, 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.pat h.dirname(os.pa th.abspath(__fi le__)))
sys.argv = [sys.argv[0], 'build_ext', '-i']
setup(ext_modul es = [Extension('_hel lo', ["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.comwrot e:
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********@gmai l.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
2309
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 have so far:
2
3121
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 not? Thanks for hints, Torsten.
2
1966
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
13507
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
2386
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: /usr/lib/python2.4/lib-dynload/math.so: undefined symbol: PyExc_FloatingPointError The module is as follows:
7
2410
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 the Python functions to 'see' the Dict created in the C++ App. The C++ app creates the Dict using the following functions:
2
3672
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 does not work :-\ Here is my simplified code:
3
2696
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 * dummy = NULL; double * internal_list; <snip and forget the rest>
0
1115
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 example code was taken from pg 1478 of the 3rd edition python o'reilly book. I cannot see what I'm doing wrong here to get this segfault? Please help. //-------------------python-code-------------------------// #! /usr/bin/env python...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10580
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.