PDA

View Full Version : Importing c++ into python



dodle
April 11th, 2009, 02:52 AM
Can I run a C++ program from within the python interpreter or a program?

I want to run the simple C++ Hello World program, that prints "Hello World" to the command line, from python.

testc.cpp

#include <iostream>
using namespace std;

int main()
{
cout << "Hello World\n";
return 0;
}
How would I import and run the compiled testc?

Edit: Or am I supposed to get the c++ functions from the source?

pmasiar
April 11th, 2009, 09:57 PM
In Python, you can execute any external program

WW
April 12th, 2009, 01:07 AM
Check out the subprocess (http://docs.python.org/library/subprocess.html) module. The simplest version is probably the call() function. Here's a quick demonstration:


$ ls
hello.cpp
$ g++ hello.cpp -o hello
$ ./hello
Hello World
$ python
EPD_Py25 (4.2.30201_beta1) -- http://www.enthought.com

Python 2.5.4 |EPD_Py25 4.2.30201_beta1| (r254:67916, Mar 27 2009, 01:52:11)
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import call
>>> status = call("./hello")
Hello World
>>> status
0
>>>

happysmileman
April 12th, 2009, 03:30 AM
As posted above you can just call("./programname") to do it.

If you want to actually be able to INTERFACE with C++ code, you can code Python Modules in C/C++ and import them just as you would any other python module.

Brief description can be found here (http://www.python.org/doc/ext/intro.html). Even if all you need right now is to call("./programname") it might be good to remember that it's possible to do this for the future, if you need to incorporate C/C++ into a Python program.

nvteighen
April 12th, 2009, 12:10 PM
If you want to actually be able to INTERFACE with C++ code, you can code Python Modules in C/C++ and import them just as you would any other python module.


Hmm... At least for C libraries (which would be the equivalent for "modules"), you have to use the ctypes module in order to be able to interface Python with C data types. Look at: http://docs.python.org/library/ctypes.html

No idea about C++, but it should be similar, I guess.