包含
using namespace boost::python;
struct World{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
编译和构建成功
~/boost$ g++ -fPIC -I/usr/include/python2.6 -c hello.cpp
~/boost$ g++ -shared hello.o -o hello.so
但是当从Python侧导入时,会出现错误。
>>> import hello.so
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./hello.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv
>>>
ld
编译时链接器是单遍链接器:它从命令行从左到右选取要解决的符号,并维护一个未解决符号的列表。因此,如果将hello.o
放在末尾,会引入新的未解决符号(这些符号在libpython2.6.so
和libboost_python.so
中被定义),但现在无法解决这些符号,因为没有任何向右定义这些符号的内容。 - Emmet