Under my Ubuntu:
$ cat test.py
#Filename test.py def Hello(): print "Hello, world!"
$ cat tom.cpp
#include <Python.h> int main() { Py_Initialize(); PyObject * pModule = NULL; PyObject * pFunc = NULL; pModule = PyImport_ImportModule("test"); pFunc = PyObject_GetAttrString(pModule, "Hello"); PyEval_CallObject(pFunc, NULL); Py_Finalize(); return 0; }
And then compile it:
g++ tom.cpp -I/usr/include/python2.7 -L/usr/lib/python2.7 -lpython2.7
Run: $. / A.out
Segmentation fault
Why? Can anyone help? Thanks!
BR, Tom
source share