Embedding Python

M

Markus Franz

Hi!

I wanted to run some Python code from inside a C program, so I did it
like it was explained in the Python manual:

#include <Python.h>
int main(int argc, char *argv[]) {
Py_Initialize();
PyRun_SimpleString("print 'Hallo World!'\n");
Py_Finalize();
return 0;
}

Then I tried to compile this by using the command: gcc halloworld.cpp
But everytime I get the following errors:

/home/mmf/tmp/ccVD2V4h.o(.text+0x1d): In function `main':
: undefined reference to `Py_Initialize'
/home/mmf/tmp/ccVD2V4h.o(.text+0x2a): In function `main':
: undefined reference to `PyRun_SimpleString'
/home/mmf/tmp/ccVD2V4h.o(.text+0x32): In function `main':
: undefined reference to `Py_Finalize'
/home/mmf/tmp/ccVD2V4h.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status

What am I doing wrong?

Markus
 
D

Diez B. Roggisch

/home/mmf/tmp/ccVD2V4h.o(.text+0x1d): In function `main':
: undefined reference to `Py_Initialize'
/home/mmf/tmp/ccVD2V4h.o(.text+0x2a): In function `main':
: undefined reference to `PyRun_SimpleString'
/home/mmf/tmp/ccVD2V4h.o(.text+0x32): In function `main':
: undefined reference to `Py_Finalize'
/home/mmf/tmp/ccVD2V4h.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status

What am I doing wrong?

Not linking against libpython.so?
 
D

Diez B. Roggisch

Markus said:
I don't understand what you mean...

Well, you need to link against the python library to make known where the
respective symbols are from. That is also necessary for all other libs out
there - e.g. the linker switch -lm links against libm.so. For someone who
_codes_ in C/C++ that should be obvious - or so I thought. What
build-environment do you use?

The line

suggest that you also miss linking against libstdc++ - an error that
sometimes happened to me too - no idea why, it _should_ be included
implicitely when linking C++ objects. No big deal though, just also add it
to the linking process.
 
M

Mark Tolonen

Markus Franz said:
Hi!

I wanted to run some Python code from inside a C program, so I did it
like it was explained in the Python manual:

#include <Python.h>
int main(int argc, char *argv[]) {
Py_Initialize();
PyRun_SimpleString("print 'Hallo World!'\n");
Py_Finalize();
return 0;
}

Then I tried to compile this by using the command: gcc halloworld.cpp
But everytime I get the following errors:

/home/mmf/tmp/ccVD2V4h.o(.text+0x1d): In function `main':
: undefined reference to `Py_Initialize'
/home/mmf/tmp/ccVD2V4h.o(.text+0x2a): In function `main':
: undefined reference to `PyRun_SimpleString'
/home/mmf/tmp/ccVD2V4h.o(.text+0x32): In function `main':
: undefined reference to `Py_Finalize'
/home/mmf/tmp/ccVD2V4h.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status

What am I doing wrong?

Markus

This works for me on Redhat 9:

g++ x.cpp -o x -I/usr/include/python2.2 -pthread -lm
-ldl -lutil /usr/lib/python2.2/config/libpython2.2.a

I discovered what libraries to link to by asking distutils:

import distutils.sysconfig
distutils.sysconfig.get_config_var('LIBS')
distutils.sysconfig.get_config_var('SYSLIBS')

g++ links to the right libraries for .cpp files.

-Mark
 
D

Diez B. Roggisch

This works for me on Redhat 9:
g++ x.cpp -o x -I/usr/include/python2.2 -pthread -lm
-ldl -lutil /usr/lib/python2.2/config/libpython2.2.a

Why did you chose the static variant? This should be equivalent:

g++ x.cpp -o x -I/usr/include/python2.2 -pthread -lm
-ldl -lutil -lpython2.2
 
M

Mark Tolonen

Diez B. Roggisch said:
Why did you chose the static variant? This should be equivalent:

g++ x.cpp -o x -I/usr/include/python2.2 -pthread -lm
-ldl -lutil -lpython2.2

On my system, for whatever reason, the .so library isn't present. I have
the python-devel package installed.

-Mark
 
R

Reinhold Birkenfeld

Heiko said:
Am Samstag, 26. März 2005 20:43 schrieb Mark Tolonen:

I actually can't believe this; do

ldconfig -p|grep "python"

Or, use

ldd =python

to exactly display what library your current executable is using.

(users of a shell other than Zsh must replace '=python' by '`which python`')


Reinhold
 
M

Mark Tolonen

Reinhold Birkenfeld said:
Or, use

ldd =python

to exactly display what library your current executable is using.

(users of a shell other than Zsh must replace '=python' by '`which
python`')


Reinhold

$ ldd /usr/bin/python
libdl.so.2 => /lib/libdl.so.2 (0x40023000)
libpthread.so.0 => /lib/tls/libpthread.so.0 (0x40028000)
libutil.so.1 => /lib/libutil.so.1 (0x40036000)
libm.so.6 => /lib/tls/libm.so.6 (0x40039000)
libc.so.6 => /lib/tls/libc.so.6 (0x42000000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

I also (before I originally posted) did a "find / -name libpython*" with no
success. Looks like Redhat 9 ships with a statically linked version of
python.

-Mark
 
H

Heiko Wundram

Am Samstag, 26. März 2005 20:43 schrieb Mark Tolonen:
On my system, for whatever reason, the .so library isn't present. I have
the python-devel package installed.

I actually can't believe this; do

ldconfig -p|grep "python"

as root and look for any output. And remember that the shared library isn't
installed by the devel package, but by the standard python package, as the
binary /usr/bin/python is only a "stub", which chains to the python
interpreter in the shared lib (at least for any distribution I know of, it
would be braindead to link the command-line interpreter statically anyway).

Sample output:

heiko heiko # ldconfig -p|grep python
libpython2.4.so.1.0 (libc6) => /usr/local/lib/libpython2.4.so.1.0
libpython2.4.so (libc6) => /usr/local/lib/libpython2.4.so
libpython2.3.so.1.0 (libc6) => /usr/lib/libpython2.3.so.1.0
libpython2.3.so (libc6) => /usr/lib/libpython2.3.so
heiko heiko #

--
--- Heiko.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBCRczAf0bpgh6uVAMRAv0AAJ9ro3+QmTIQDgg4/rnuApsqRHffbgCfUCh/
BZFYiU8h4u6Ya9SCYyZy94A=
=RrOj
-----END PGP SIGNATURE-----
 
H

Heiko Wundram

Am Samstag, 26. März 2005 21:36 schrieb Mark Tolonen:
I also (before I originally posted) did a "find / -name libpython*" with no
success. Looks like Redhat 9 ships with a statically linked version of
python.

Hmm... Sorry to have thought otherwise... RedHat is braindead. :)

--
--- Heiko.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD4DBQBCRdlbf0bpgh6uVAMRAmImAJ4q8n+pgUDQ65bDobXDsunKA8fi9wCY8N3m
mBDvW/jcOEAAfIU4FQm7Jg==
=wnlO
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top