473,722 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_SimpleStr ing("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(.tex t+0x1d): In function `main':
: undefined reference to `Py_Initialize'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
: undefined reference to `PyRun_SimpleSt ring'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
: undefined reference to `Py_Finalize'
/home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
`__gxx_personal ity_v0'
collect2: ld returned 1 exit status

What am I doing wrong?

Markus
Jul 18 '05 #1
10 4220
> /home/mmf/tmp/ccVD2V4h.o(.tex t+0x1d): In function `main':
: undefined reference to `Py_Initialize'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
: undefined reference to `PyRun_SimpleSt ring'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
: undefined reference to `Py_Finalize'
/home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
`__gxx_personal ity_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(.tex t+0x1d): In function `main':
: undefined reference to `Py_Initialize'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
: undefined reference to `PyRun_SimpleSt ring'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
: undefined reference to `Py_Finalize'
/home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
`__gxx_person ality_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(.tex t+0x1d): In function `main':
: undefined reference to `Py_Initialize'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
: undefined reference to `PyRun_SimpleSt ring'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
: undefined reference to `Py_Finalize'
/home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
`__gxx_perso nality_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_perso nality_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.d e> wrote in message
news:57******** *************** ***@posting.goo gle.com...
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_SimpleStr ing("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(.tex t+0x1d): In function `main':
: undefined reference to `Py_Initialize'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
: undefined reference to `PyRun_SimpleSt ring'
/home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
: undefined reference to `Py_Finalize'
/home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
`__gxx_personal ity_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.sysco nfig
distutils.sysco nfig.get_config _var('LIBS')
distutils.sysco nfig.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*********@we b.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)

iD8DBQBCRczAf0b pgh6uVAMRAv0AAJ 9ro3+QmTIQDgg4/rnuApsqRHffbgCf UCh/
BZFYiU8h4u6Ya9S CYyZy94A=
=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************ ************@wo lke7.net> wrote in
message news:3a******** *****@individua l.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2788
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 working with Python for a year or more now, but only doing simple extending in C/C++. I'm now attempting some embedding and several questions have come to mind. BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
2
2869
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 embedded scripting language (a very limited one). I would like to rip it out and replace it with Python. I am thinking that this would be relatively simple since the scripting language is a very small interface between the UI and the engine --...
1
1918
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 questions about python embedding: 1) Is there a good text book or other resource on embedding/extending? (I find it hard to learn only by the tutorial and C/Python API from the python.org site)
6
3004
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 interface is providing a convenient scripting toolkit for the application. One example is that a user can write a python script like: player = Player() game.loadPlayer(player) player.moveTo(location)
0
2113
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 callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call Register_Handler(...). I thought there was some magic here that was assigning the pointer *Handler to my python...
0
8861
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
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
9383
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
9236
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
9088
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
8051
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...
0
5995
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
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.