我正在嘗試使用boost.python構建一個簡單的程序。
我有以下代碼:無法使用boost.python導入模塊
//greet.cpp
#include <iostream>
#include <boost/python.hpp>
void greet()
{
std::cout << "hello world!" << std::endl;
}
BOOST_PYTHON_MODULE(greet)
{
using namespace boost::python;
def("greet", greet);
}
和follwing makefile
:
PYTHON_VERSION := 2.7
PYTHON_INC := /usr/include/python$(PYTHON_VERSION)
PYTHON_LIB_LOCATION := /usr/lib/python${PYTHON_VERSION}
PYTHON_LIB_FILE := python${PYTHON_VERSION}
BOOST_INC := ~/boost_1_54_0
BOOST_LIB_LOCATION := /home/elyashiv/boost_1_54_0/stage/lib
BOOST_LIB_FILE := boost_python
CC := gcc
CFLAGS := -c -fPIC
CInc := -I ${BOOST_INC} -I ${PYTHON_INC}
CLinkFlags = -shared -Wl,-soname,[email protected] -L${BOOST_LIB_LOCATION} -l${BOOST_LIB_FILE} -L${PYTHON_LIB_LOCATION} -l${PYTHON_LIB_FILE}
greet.o: greet.cpp
%.so: %.o
gcc ${CLinkFlags} -o [email protected] $^
%.o: %.cpp
${CC} ${CFLAGS} ${CInc} $^
運行make greet.so
運行,只需幾個警告(重新定義一些提振文件)。
當我嘗試導入模塊中的蟒蛇,我得到如下:
Python 2.7.3 (default, Apr 10 2013, 05:46:21)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import greet
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./greet.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv
是我做錯了,如何解決?
編輯
的ldd greet.so
輸出:
linux-gate.so.1 => (0x001ee000)
libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0x0055d000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0x0058e000)
/lib/ld-linux.so.2 (0x003a2000)
如果你在Linux上,你是否也可以粘貼'ldd greet.so'的輸出?這是否顯示你的boost_python庫鏈接? –
@AndréAnjos補充說。 – elyashiv
'libboost_python'在哪裏?它沒有鏈接,因此,你有缺失的符號。確保您的編譯按預期工作。 –