Unix

Python 3.2 embedding error on dynamic linking

알 수 없는 사용자 2012. 3. 20. 21:32
Order matters...

Say main.cpp is the example embedding code at  http://docs.python.org/py3k/extending/embedding.html .
 Here is a wrong Makefile:
test: a.out


# Run python3.2-config --ldflags command to get required flags!
PYTHON_LDFLAGS=-L/usr/lib/python3.2/config-3.2mu \
-lpthread -ldl -lutil -lm -lpython3.2mu \
-Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions

# Run python3.2-config --cflags command to get required flags!
PYTHON_CFLAGS=-I/usr/include/python3.2mu -g -fwrapv -Wall

# You must put linking flag AFTER the input source code...
a.out: main.cpp
g++ ${PYTHON_CFLAGS} ${PYTHON_LDFLAGS} main.cpp

The error is like following:
/home/xxx/work/py_embed/main.cpp:11: undefined reference to `Py_Initialize'
/home/xxx/work/py_embed/main.cpp:14: undefined reference to `PyUnicodeUCS4_FromString'
/home/xxx/work/py_embed/main.cpp:15: undefined reference to `PyImport_Import

I don't know exactly why this happens but I know how to get over this error. Modify the Makefile like this:
a.out: main.cpp
g++ ${PYTHON_CFLAGS} main.cpp ${PYTHON_LDFLAGS} 
Linker flags should come last.