How to embed Python in C program?

7 posts / 0 new
Last post
jap
jap's picture
Offline
Last seen: 1 month 2 weeks ago
Joined: 2011-06-04 05:53
How to embed Python in C program?

Hello,

I'm trying to include the Python interpreter in my C program to execute some Python code:

  1. /* main.c */
  2.  
  3. #include <python2.5/Python.h>
  4.  
  5. int main ( int argc, char* argv [] ) {
  6. Py_Initialize ();
  7. PyRun_SimpleString ( "print ( 'Hello world!' )" );
  8. Py_Finalize ();
  9.  
  10. return ( 0 );
  11. }

I'm having trouble compiling and linking the C program. If I've understood correctly, I need to link libpython25.so library to my program to make it work. I've tried compiling it with these parameters

gcc -Wall -llibpython25 -oMyPythonTest main.c

but I get this error:

ld: cannot find -llibpython25

hypex
hypex's picture
Offline
Last seen: 1 month 1 day ago
Joined: 2011-09-09 16:20
Re: How to embed Python in C program?

A few things here. You don't need to specify "lib" so using "-lpython25" is fine.

If you really need to use a .so then you will need to specify "-use-dynld" on the compile line like so:

  1. gcc -Wall -o MyPythonTest.o -c main.c
  2. gcc -use-dynld -o MyPythonTest MyPythonTest.o -lpython25

Check the AmigaOS 4.1 SDK.pdf in the SDK root for more info.

jap
jap's picture
Offline
Last seen: 1 month 2 weeks ago
Joined: 2011-06-04 05:53
Re: How to embed Python in C program?

Thanks! I managed to compile and link the program but not without issues.
The linker notified that libpython25.so needs these libraries also:

  • libdl.so
  • libexpat.so
  • libz.so.1
  • libz.so
  • libpthread.so

So I tried linking all those libraries to my program:

  1. gcc -Wall -o MyPythonTest.o -c main.c
  2. gcc -use-dynld -lpython25 -lexpat -lpthread -ldl -lz -o MyPythonTest MyPythonTest.o

The linker complained that it cannot find libz.so and libz.so.1 libraries. Only after copying libz.so from SOBJS: to SDK:local/newlib/lib/ solved the problem.

hypex
hypex's picture
Offline
Last seen: 1 month 1 day ago
Joined: 2011-09-09 16:20
Re: How to embed Python in C program?

Good it's compiling now at least. That's strange it can't find it in SOBJ: path. The SDK guide mentions four search paths. As useful as the so objects are I think they complicate the program for users. The Amiga has always had a major problem with no package manager. I installed a program that needed all these objects that weren't supplied and after tracking a few down, I found it still wouldn't load and I had to hack the files in my SOBJ: for it to work. Nice idea but I think they needed more work when implementing the objects.

Have you tried getting out Snoopy to track the loading fails?

If there was a static python lib it would be easier. :-)

xenic
xenic's picture
Offline
Last seen: 3 months 3 weeks ago
Joined: 2011-05-07 04:52
Re: How to embed Python in C program?

On my system libz.so and libz.so.1 are links while the objects that the linker had no problem with are actual files. When libz.so was copied, the resolved link (actual file) was probably copied. Possibly there is a problem with links??

X1000 - OS 4.1FE
jap
jap's picture
Offline
Last seen: 1 month 2 weeks ago
Joined: 2011-06-04 05:53
Re: How to embed Python in C program?

Have you tried getting out Snoopy to track the loading fails?

No I haven't.

I have these file links in the SDK:local/newlib/lib/ directory now

  • libcairo.so
  • libdl.so
  • libexpat.so
  • libfontconfig.so
  • libfreetype.so
  • libpixman-1.so
  • linpng12.so
  • libpthread.so
  • libpython25.so
  • libz.so.1.2.3

and these files

  • libdl.so
  • libz.so
  • libz.so.1
  • libamisslauto.a
  • libexpat.a
  • libfl.a
  • libGL.a
  • libGLU.a
  • libglut.a
  • libpthread.a
hypex
hypex's picture
Offline
Last seen: 1 month 1 day ago
Joined: 2011-09-09 16:20
Re: How to embed Python in C program?

I have these file links in the SDK:local/newlib/lib/ directory now

I have the same and they all point to SOBJS:

and these files

I have most except I only have one libz.so named libz.so.1.2.3. I have all the rest.

Also, the ".a" lib files won't matter here as you are using ".so" objects. Though it can be done, with some work, they don't recommend linking both ".a" and ".so" types of libs into a binary.

Log in or register to post comments