473,320 Members | 1,947 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,320 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 4143
> /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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.