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

Embedding Python

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
> /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?
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
Diez B. Roggisch wrote:
/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?


I don't understand what you mean...

Markus
Jul 18 '05 #3

P: n/a
Markus Franz wrote:
Diez B. Roggisch wrote:
/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?


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
/home/mmf/tmp/ccVD2V4h.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'


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.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #4

P: n/a

"Markus Franz" <mf****@arcor.de> wrote in message
news:57**************************@posting.google.c om...
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
Jul 18 '05 #5

P: n/a
> 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

--
Regards,

Diez B. Roggisch
Jul 18 '05 #6

P: n/a

"Diez B. Roggisch" <de*********@web.de> wrote in message
news:d2*************@news.t-online.com...
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

--
Regards,

Diez B. Roggisch


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

-Mark
Jul 18 '05 #7

P: n/a
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-----

Jul 18 '05 #8

P: n/a
Heiko Wundram wrote:
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"


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
Jul 18 '05 #9

P: n/a

"Reinhold Birkenfeld" <re************************@wolke7.net> wrote in
message news:3a*************@individual.net...
Heiko Wundram wrote:
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"


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
Jul 18 '05 #10

P: n/a
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+pgUDQ65bDobXDsu nKA8fi9wCY8N3m
mBDvW/jcOEAAfIU4FQm7Jg==
=wnlO
-----END PGP SIGNATURE-----

Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.