gpt4 book ai didi

python - 我应该包括什么内容以进行boost.python扩展?

转载 作者:行者123 更新时间:2023-12-02 10:13:44 24 4
gpt4 key购买 nike

编辑:意外复制粘贴一行两次。
我正在使用boost和常规C API的组合进行C++ Python扩展,但是我无法对其进行编译。我已阅读以下文档:
documentation from boost.org
documentation from python wiki
Python bindings for C++ medium
这是cpp文件中的代码:

#include <boost/python.hpp>#include <boost/python/make_constructor.hpp>#include <boost/python/detail/api_placeholder.hpp>#include <iostream>#include <string>using namespace std;class TestClass{    TestClass(string msg){        cout << "Created test class object" << endl;        cout << msg;    }};BOOST_PYTHON_MODULE(packet){    class_<TestClass>("TestClass", init<std::string>());}

the makefile:

test: test.cpp
g++ -Wall -shared -std=c++11 -fPIC -I/usr/include -o test`python3-config --extension-suffix` test.cpp
错误输出:
test.cpp:17:5: error: ‘class_’ was not declared in this scope
class_<TestClass>("TestClass", init<std::string>());
^
test.cpp:17:5: note: suggested alternative:
In file included from /usr/include/boost/python/object_core.hpp:20:0,
from /usr/include/boost/python/args.hpp:25,
from /usr/include/boost/python.hpp:11,
from test.cpp:1:
/usr/include/boost/python/def_visitor.hpp:14:56: note: ‘boost::python::class_’
template <class T, class X1, class X2, class X3> class class_;
^
test.cpp:17:21: error: expected primary-expression before ‘>’ token
class_<TestClass>("TestClass", init<std::string>());
^
test.cpp:17:36: error: ‘init’ was not declared in this scope
class_<TestClass>("TestClass", init<std::string>());
^
test.cpp:17:36: note: suggested alternative:
In file included from /usr/include/boost/python/class.hpp:20:0,
from /usr/include/boost/python.hpp:18,
from test.cpp:1:
/usr/include/boost/python/init.hpp:58:7: note: ‘boost::python::init’
class init; // forward declaration
^
test.cpp:17:52: error: expected primary-expression before ‘>’ token
class_<TestClass>("TestClass", init<std::string>());
^
test.cpp:17:54: error: expected primary-expression before ‘)’ token
class_<TestClass>("TestClass", init<std::string>());
^
make: *** [test] Error 1

我想我已经包含了所有头文件,但是我不确定为什么它说没有在此范围内声明。任何帮助将不胜感激

最佳答案

就像(几乎?)Boost中的所有内容(不是宏)一样,class_在命名空间中,在本例中为boost::python。要么:

  • 在文件顶部(包括之后)添加using namespace boost::python;
  • 改为引用boost::python::class_

  • 其他符号也是如此。
    这是 shown in the quickstart part of the documentation。对于其余大部分,它们仅显示短代码片段,因此我认为它是隐式的。

    关于python - 我应该包括什么内容以进行boost.python扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62627838/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com