Calling C++ from Python

Jeff Shannon jeff at ccvcorp.com
Fri Aug 17 20:12:36 CEST 2001


Rbtns wrote:

> How would I call a C++ object from Python?
>
> I have a file of something like:
>
> #include <iostream.h>
> int main()
> {
>   cout <<"I need this to work\n";
>   return 0;
> }
>
> and I want to display my C++ program output from within a python
> program. Do I create an object of the C++ program, and therefore would
> it need to be in a class?  How do I do this if I don't want the program
> in a class?  How do I import the C++ file?

If you have an existing program that you wish to start from within a Python
program, there are several options, mostly involving the os module.
os.system('myprog.exe') will start a separate process that runs
myprog.exe.  If you want Python to be able to send input to, and receive
output from, the other program, then you'd want to use os.popen() or one of
its many variants.  (Note that popen() is broken on windows, you'll want to
use the replacement proveded in the win32 extensions.)  Check your library
reference for details.

Jeff Shannon
Technician/Programmer
Credit International






More information about the Python-list mailing list