【发布时间】:2020-12-27 10:24:40
【问题描述】:
我在关注https://www.boost.org/doc/libs/1_74_0/libs/python/doc/html/tutorial/tutorial/exposing.html。但是 boost 教程并没有说明如何构建它。它建议使用 b2 所以我按照上面链接的上一页中的说明进行操作。 (我在 $PATH 中添加了 b2 并在 /usr/local/lib 下为 libboost_python.so 创建了一个符号链接)。我曾经编译并运行过这个 world.cpp 示例(请参阅 exposing C++ class in Python ( only ET_DYN and ET_EXEC can be loaded)),但这次它无法以某种方式工作,现在我正在尝试使用 b2 来实现。
这是 world.cpp 文件。
#include <boost/python.hpp>
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(world_ext)
{
using namespace boost::python;
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
这里是 Jamfile。
# Copyright Stefan Seefeld 2016.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
import python ;
project tutorial
: requirements
<location>.
;
# ------ for hello
#python-extension hello_ext : hello.cpp ;
#
#run-test hello : hello_ext hello.py ;
#
#alias test : hello ;
#explicit test ;
# ------ for world
python-extension world_ext : world.cpp ;
run-test world : world_ext world.py ;
alias test : world ;
explicit test ;
当我运行 b2 时,它给了我下面的错误消息。(编辑我在 BOOST_PYTHON_MODULE() 行中将 world 修复为 world_ext 并在上面的代码中添加了 using namespace boost::python;。)
...found 11 targets...
...updating 4 targets...
gcc.compile.c++ world.o
world.cpp: In function 'void init_module_world_ext()':
world.cpp:20:17: error: wrong number of template arguments (1, should be 4)
class_<World>("World")
^
In file included from /usr/local/include/boost/python/object_core.hpp:20:0,
from /usr/local/include/boost/python/args.hpp:22,
from /usr/local/include/boost/python/make_function.hpp:11,
from /usr/local/include/boost/python/def.hpp:11,
from world.cpp:8:
/usr/local/include/boost/python/def_visitor.hpp:14:56: note: provided for 'template<class T, class X1, class X2, class X3> class boost::python::class_'
template <class T, class X1, class X2, class X3> class class_;
^
"g++" -fPIC -O0 -fno-inline -Wall -g -I"/usr/include/python3.5" -c -o "world.o" "world.cpp"
...failed gcc.compile.c++ world.o...
...skipped <p.>world_ext.so for lack of <p.>world.o...
...skipped <p.>world for lack of <p.>world_ext.so...
...failed updating 1 target...
...skipped 3 targets...
如果我在 Jamfile 中注释掉 world 部分和取消注释hello 部分,b2 工作正常,我可以运行 hello.py。 hello.cpp、hello.py、world.cpp、world.py 文件都在同一个目录下。 (~/BOOST/boost_1_73_0/libs/python/example/tutorial)
看起来是关于暴露类的。它需要 4 个模板参数,但原始教程代码只提供了一个。我该怎么办? (顺便说一下,我希望boost的人更新当前boost版本的教程,有更多的解释。文档和发布的代码之间存在很多差异。)
【问题讨论】:
标签: python c++ boost linker boost-python