ubuntu中c++调用python

新博客地址:http://gorthon.sinaapp.com/

先写一个test.cpp:

#include<Python.h> int main(int argc, char **argv) { Py_Initialize(); PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); if(!Py_IsInitialized()) { printf("init failed/n"); } PyObject *pModule=NULL; PyObject *pFunc=NULL; pModule=PyImport_ImportModule("test"); pFunc=PyObject_GetAttrString(pModule,"run_test"); PyEval_CallObject(pFunc,NULL); Py_Finalize(); return 0; }

 

#test.py:

print 'hello c++' 

g++编译

g++ test.cpp -I /usr/include/python2.6 -L /usr/lib/python2.6 -lpython2.6

你可能感兴趣的:(ubuntu中c++调用python)