尝试导入使用 boost python 编译的扩展时出现未定义符号错误,该符号应该包含在 boost 库中。
我使用的是 Boost 1.46.1、Python 3.1.2 和 GCC 4.4.5。
我使用以下方法构建了 boost:
$ ./bootstrap.sh --with-python-version=3.1
$ sudo ./bjam -j4 install
Run Code Online (Sandbox Code Playgroud)
然后我编译了以下简单的 Boost Python 库:
#include <boost/python.hpp>
struct mystruct {
int i;
};
BOOST_PYTHON_MODULE(test) {
using namespace boost::python;
class_<mystruct>("Mystruct")
.def_readwrite("i", &mystruct::i)
;
}
Run Code Online (Sandbox Code Playgroud)
使用命令:
$ g++ -shared question.cpp -I/usr/include/python3.1 -lboost_python3 -lpython3.1 -otest.so
Run Code Online (Sandbox Code Playgroud)
成功而没有错误。
然后我尝试在 python 中运行它,但它似乎找不到 init_module 函数 boost python 应该提供:
$ python3
Python 3.1.2 (release31-maint, Sep 17 2010, 20:34:23)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" …Run Code Online (Sandbox Code Playgroud) 如何使用 Boost.Python 导出非默认构造类?
这段代码:
class EventHandle {
public:
EventHandle() = delete;
EventHandle(boost::shared_ptr<EventManager> const& em): event_manager_(em) {}
EventHandle(EventHandle const&) = delete;
~EventHandle();
shared_ptr<EventManager> event_manager_;
}
class_<EventHandle, noncopyable,
init<boost::shared_ptr<EventManager> const&>>("EventHandle")
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
/opt/local/include/boost/python/pointee.hpp: In instantiation of 'boost::python::detail::pointee_impl<false>::apply<boost::python::init<const boost::shared_ptr<EventManager>&> >':
/opt/local/include/boost/python/pointee.hpp:38:1: instantiated from 'boost::python::pointee<boost::python::init<const boost::shared_ptr<EventManager>&> >'
/opt/local/include/boost/mpl/eval_if.hpp:38:31: instantiated from 'boost::mpl::eval_if<boost::is_convertible<boost::python::init<const boost::shared_ptr<EventManager>&>*, EventHandle*>, boost::mpl::identity<boost::python::init<const boost::shared_ptr<EventManager>&> >, boost::python::pointee<boost::python::init<const boost::shared_ptr<EventManager>&> > >'
/opt/local/include/boost/python/object/class_metadata.hpp:179:13: instantiated from 'boost::python::objects::class_metadata<EventHandle, boost::noncopyable_::noncopyable, boost::python::init<const boost::shared_ptr<EventManager>&>, boost::python::detail::not_specified>'
/opt/local/include/boost/python/class.hpp:174:42: instantiated from 'boost::python::class_<EventHandle, boost::noncopyable_::noncopyable, boost::python::init<const boost::shared_ptr<EventManager>&> >::id_vector'
/opt/local/include/boost/python/class.hpp:627:55: instantiated from 'boost::python::class_<T, X1, X2, X3>::class_(const char*, const …Run Code Online (Sandbox Code Playgroud) 我制作了一个带有无限循环的 boost.python 模块。但是我无法通过 ctrl-c 终止该进程。以下是一个示例。
C++
#include <boost/python.hpp>
#include <boost/python.module.hpp>
#include <boost/python.def.hpp>
#include <iostream>
usring namespace boost::python;
void foo() {
int it=0;
while (true) { //endless loop
++it;
std::cout<< it <<std::endl;
sleep(3);
}
}
BOOST_PYTHON_MODULE(ctopy)
{
def("foo",foo);
}
Run Code Online (Sandbox Code Playgroud)
Python:
import ctopy
ctopy.foo()
Run Code Online (Sandbox Code Playgroud)
结果:
1
2
3
4
.....................
Run Code Online (Sandbox Code Playgroud)
我无法通过 Ctrl-c 杀死前台进程。为什么模块不接受 Ctrl-c 发送的信号“SIGINT”。如何使其工作。
我正在围绕 ArUco 增强现实库(基于 OpenCV)编写一个薄包装。我正在尝试构建的界面非常简单:
但是,我无法弄清楚如何在 Python 中表示图像以将其传递给 C++。对于 GUI 和相机管理,我将使用 PyQt,所以最初它将是 QImage,但我不能简单地将它传递给 OpenCV(或者我可以?)。一开始,我尝试使用嵌套元组来表示每个像素的行、列和颜色,所以我最终得到了这个示例代码:
using namespace cv;
namespace py = boost::python;
void display(py::tuple pix)
{
/*
Receive image from Python and display it.
*/
Mat img(py::len(pix), py::len(pix[0]), CV_8UC3, Scalar(0, 0, 255));
for (int y = 0; y < py::len(pix); y++)
for (int x = 0; x < py::len(pix[y]); x++)
{
Vec3b rgb;
for (int i = 0; i < 3; i++)
rgb[i] = py::extract<int>(pix[y][x][i]); …Run Code Online (Sandbox Code Playgroud) 当我在main函数中使用 boost::python::tuple 或 boost::python::dict 时,程序崩溃了!
#define BOOST_PYTHON_STATIC_LIB
#include <boost/python.hpp>
#include <iostream>
#include <boost/python/tuple.hpp>
#include <boost/python/dict.hpp>
//using namespace std;
using namespace boost::python;
int main()
{
//tuple x;
//x = make_tuple(object(0),object(1));
dict x;
x["123"] = 3;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我在 中使用它们时.dll,没关系,有什么问题?
首先,我要感谢大家努力解决我的这个疑问。我正在转换一个最小的 C++ 项目以在 Python 中使用。这种努力背后的真正原因是为了速度。
我遇到了 PyBind,对它的功能以及他们提供的文档数量感到非常惊讶。现在有一些事情阻止了工作,因为我不知道该怎么做。考虑文件“MySource.hpp”中的以下代码,您能告诉我如何完成绑定吗?
struct Point3D
{
public:
double x, y, z;
CPoint3D();
CPoint3D(double x, double y, double z);
inline double Len() const;
inline void Normalize();
};
Point3D VectorCross(const Point3D& pt1, const Point3D& pt2, const Point3D& pt3);
void VectorCross(const float* u, const float* v, float * n);
Run Code Online (Sandbox Code Playgroud)
我能够将 Point3D 的绑定定义为一个类及其某些成员函数。但我不知道如何为重载方法“ VectorCross ”进行绑定。它有两种方法,一种接受 Point3D 的实例,另一种接受指向浮点数组的指针。
到目前为止我写的绑定如下所示
PYBIND11_MODULE(mymodule, m)
{
py::class_<Point3D> point3d(m, "Point3D");
point3d.def_readwrite("x", &CPoint3D::x);
point3d.def_readwrite("y", &CPoint3D::y);
point3d.def_readwrite("z", &CPoint3D::z);
point3d.def(py::init<>());
point3d.def(py::init<double , double , double >());
point3d.def("Len", &CPoint3D::Len);
point3d.def("Normalize", …Run Code Online (Sandbox Code Playgroud) 我不会将此添加为答案,因为我仍然没有从技术上解决问题。但由于我现在花了 2.5 天尝试让 boost-python3 工作起来,我已经失去了忍受它的意愿。
我刚刚遇到了pybind11(我之前对 python 绑定工具的长时间搜索没有找到它,我不知道)并且正在使用它。2.5 天的痛苦与安装和构建cmake 示例的不到 20 分钟相比......并且所有特定的 python 版本依赖性地狱都消失了。
它在语法上与 boost-python 类似,但更易于管理、速度更快、仅包含标头并且功能更丰富。
耶!
我正在使用 boost::python 绑定 python 3.7.2 中的类。
类导入成功,但实例化它会出现以下错误:
<my-terminal>$ python
Python 3.7.2 (default, Feb 14 2019, 17:36:47)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import classes
>>> t = classes.World()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() should return None, not 'NoneType'
>>> …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Math应用程序,可以通过编写python脚本来扩展它.
我使用Qt 4.6.3(构建为静态库,调试和发布版本)和Boost 1.43.0(构建为静态库,运行时链接也设置为静态,多线程版本,调试和发布).一切都是用MSVC++ 2008构建的.Boost构建了以下库:
我的项目编译,但在链接阶段给出以下错误:
1>Linking...
1>LINK : fatal error LNK1104: cannot open file 'boost_python-vc90-mt-gd-1_43.lib'
Run Code Online (Sandbox Code Playgroud)
为什么不选择我编译的库之一?
我认为库名中的s代表静态,但是自动链接功能似乎选择了一个动态库,我希望它在一个可执行文件中静态链接.
regex库也是如此:我编译了相同的4个正则表达式库并且快速测试显示了这个链接错误:
1>LINK : fatal error LNK1104: cannot open file 'libboost_regex-vc90-mt-gd-1_43.lib'
Run Code Online (Sandbox Code Playgroud)
该怎么办?
我正在使用boost::python创建C++库的Python包装器.在某些时候,boost::python需要一个指向成员函数(或兼容的东西)的指针,如:
template <class MyClass, typename ValueType>
void (*setter_function)(MyClass&, ValueType)
// This doesn't compile, but you got the idea.
my_boost_python_call(setter_function f);
Run Code Online (Sandbox Code Playgroud)
由于我正在包装的类具有以下形式的setter:
template <class MyClass, typename ValueType>
MyClass& (MyClass::*setter_method)(ValueType)
Run Code Online (Sandbox Code Playgroud)
我写了一个"转换"功能:
template <typename MyClass, typename ValueType, setter_method<MyClass, ValueType> fluent_setter>
void nonfluent_setter(MyClass& instance, ValueType value)
{
(instance.*fluent_setter)(value);
}
Run Code Online (Sandbox Code Playgroud)
我可以这样使用:
class Foo
{
public:
Foo& bar(int value);
};
my_boost_python_call(nonfluent_setter<Foo, int, &Foo::bar>);
Run Code Online (Sandbox Code Playgroud)
到目前为止,这种方法效果很好,但我想知道是否有办法让这更加"简单"(使用).
你觉得在某种程度上可能得到这样的东西:
// return type is somehow inferred from the member function pointer
my_boost_python_call(nonfluent_setter<Foo, &Foo::bar>);
// or even a …Run Code Online (Sandbox Code Playgroud) 我想collections.namedtuple从boost :: python包装函数返回一个列表,但我不知道如何从C++代码创建这些对象.对于其他一些类型,有一个方便的包装器(例如dict),这使得这很简单,但是对于namedtuple来说却没有.做这个的最好方式是什么?
dict列表的现有代码:
namespace py = boost::python;
struct cacheWrap {
...
py::list getSources() {
py::list result;
for (auto& src : srcCache) { // srcCache is a C++ vector
// {{{ --> Want to use a namedtuple here instead of dict
py::dict pysrc;
pysrc["url"] = src.url;
pysrc["label"] = src.label;
result.append(pysrc);
// }}}
}
return result;
}
...
};
BOOST_PYTHON_MODULE(sole) {
py::class_<cacheWrap,boost::noncopyable>("doc", py::init<std::string>())
.def("getSources", &cacheWrap::getSources)
;
}
Run Code Online (Sandbox Code Playgroud)