PDA

View Full Version : Embedding multiple languages into my C program?



crazyfuturamanoob
November 10th, 2008, 03:55 PM
As title says, how could I embed Python and C++ in my C program?

WW
November 10th, 2008, 04:04 PM
Start at the source, docs.python.org: Embedding Python in Another Application (http://docs.python.org/extending/embedding.html)

crazyfuturamanoob
November 10th, 2008, 05:31 PM
Ok. Tried the example from that site just to see if it works:

#include <Python.h>

int
main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
But it doesn't seem to find/load Python.h. At least it doesn't want to compile because Py_Initialize() and Py_Finalize() aren't declared.

And I have python-dev installed. What could be the problem?

CptPicard
November 10th, 2008, 05:36 PM
By the way, just so you know.. it goes more easily the other way around. You may want to consider writing the high level in Python and calling into C. Also, using C++ from C is nastier than the other way around...

crazyfuturamanoob
November 10th, 2008, 05:43 PM
By the way, just so you know.. it goes more easily the other way around. You may want to consider writing the high level in Python and calling into C. Also, using C++ from C is nastier than the other way around...

But I want use python just as a scripting language for my game.

Here's one error I got:

error: Python.h: No such file or directory
Whops html tags too lazy to replace.

geirha
November 10th, 2008, 06:18 PM
Header files are in -dev-packages. So, if you are using python2.5, install apt://python2.5-dev and find the compilation flags you need to use with

python2.5-config --cflags
and linker flags with

python2.5-config --ldflags

pp.
November 10th, 2008, 06:27 PM
Actually, you can not embed 'languages' in a C program. Also, there's no easy way to embed lines of code of another language in your C program. You can, however, write bits and pieces of your application in other languages, compile those and call the compiled parts from your C program.

This may sound like 'mere' semantics, but I rather believe that you need to sort out the concepts involved in order to make head or tail of any sensible answers you receive in this thread.

achelis
November 10th, 2008, 06:44 PM
But I want use python just as a scripting language for my game.

Here's one error I got:

error: Python.h: No such file or directory
Whops html tags too lazy to replace.

http://ubuntuforums.org/showthread.php?t=384729

and

http://mail.python.org/pipermail/python-list/2006-August/398824.html

Worked for me :)

crazyfuturamanoob
November 10th, 2008, 06:56 PM
http://ubuntuforums.org/showthread.php?t=384729

and

http://mail.python.org/pipermail/python-list/2006-August/398824.html

Worked for me :)

Got it working, added the python flags into makefile. :)