Quantcast
Channel: Prisoner Number Six » Recipes
Viewing all articles
Browse latest Browse all 10

Calling Python from C++

$
0
0

Just a little snippet of code to show how to call python (an entire Python interpreter) from inside a C++ program:

#include <Python.h>

int main(int argc, char const *argv[])
{
Py_SetProgramName(const_cast<char*>(argv[0]));
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n" "print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}

You will need the path to the Python headers and library “libpython2.7″ (2.7 for the Python version).

In OSX I asked clang++ this way:

clang++ -I./python2.7 -L. -lpython2.7 pyfromcpp.cpp -o pyfromcpp



Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images