Undefined Symbols with Python.h
I'm trying to embed a Python script into a C++ application. Before I do such a thing though, I'm trying to run the example script.
Here's my code:
#include <Python/Python.h>
int main(int argc, char *argv[]) {
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
Compiling with g++ gives开发者_如何转开发 me:
Undefined symbols:
"_Py_Initialize", referenced from:
_main in cc2Ogphq.o
"_PyRun_SimpleStringFlags", referenced from:
_main in cc2Ogphq.o
"_Py_Finalize", referenced from:
_main in cc2Ogphq.o
ld: symbol(s) not found
I'm running Mac OSX Snow Leopard and I'm running Python 2.7.2.
You need to link to the python libraries, e.g. libpython.a
for static linkage.
This answer is old and yucky (static, eww).
distutils is the answer for most cases.
If you really insist on DIY, add this to your ~/.profile:
# Works on Lion
alias pygcc='gcc -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/ /usr/lib/python2.7/config/libpython2.7.dylib'
alias pyg++='gcc -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/ /usr/lib/python2.7/config/libpython2.7.dylib'
Usage: pyg++ yourfile.cc
pygcc someotherfile.c
of course, i'm some late with my answer , but i had a identical problem and i was needed to do some searches i use ubuntu 11, bash shell solve is:
g++ c_pyth.cpp -o c_pyth -I /usr/include/python2.7/ /usr/lib/python2.7/config/libpython2.7.so
if you don't include /usr/include/python2.7/ ,compiler won't see "Python.h"
if you don't include /usr/lib/python2.7/config/libpython2.7.so , compiler won't be able to make static executable file
精彩评论