473,386 Members | 1,795 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,386 software developers and data experts.

Embedding Python

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
10 4159
> /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
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
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

"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
> 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

"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
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
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

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

Similar topics

4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
2
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an...
1
by: amit | last post by:
Hello, I am currently studying how to embedd python. I am developing a graphical C++ application. My goal is to embedd python script that will control some kind of animation. I have some...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.