我应该包括什么来制作 boost.python 扩展?
What am I supposed to include to make a boost.python extension?
编辑:不小心复制粘贴了一行。
我正在使用 boost 和普通 C API 的组合制作 C++ Python 扩展,但我无法编译它。我已阅读以下文档:
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>());
}
生成文件:
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。对于其余大部分,它们只显示简短的代码片段,所以我认为它是隐式的。
编辑:不小心复制粘贴了一行。
我正在使用 boost 和普通 C API 的组合制作 C++ Python 扩展,但我无法编译它。我已阅读以下文档:
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>()); }
生成文件:
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。对于其余大部分,它们只显示简短的代码片段,所以我认为它是隐式的。