473,795 Members | 3,439 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 5426
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
3120
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
13504
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
2385
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
2408
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
3669
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
2694
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
1112
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
9519
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
10437
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
10214
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...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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
6780
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
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3723
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.