我一直想知道这个,但我从来没有真正得到一个明确的答案.是否可以在boost.python框架内链接另一个boost.python模块.
例如,我在boost_python_module(libA)中导出了类A,在boost_python_module(libB)中导出了函数B(A a).是否可以在libB中指定链接到libA的A.
另一种看待这个问题的方法是,现在我必须在一个模块中一次性生成所有绑定.是否可以在几个boost_python_module上逐步生成绑定.
如何使用Boost.Python公开以下类?
class C {
public:
static void F(int) {}
static void F(double) {}
};
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情:
bp::class_<C>("C")
.def("F", (void (C::*)(int))&C::F).staticmethod("F")
.def("F", (void (C::*)(double))&C::F).staticmethod("F")
;
Run Code Online (Sandbox Code Playgroud)
但是,它在Python(SystemError: initialization of libdistributions raised unreported exception)中引发了一个异常.如果我从中删除其中一个重载bp::class_,那么一切正常.我知道Boost.Python可以自动处理重载的构造函数,所以我想知道静态方法是否有类似的东西.
bp::class_<C>("C")
.def("F", (void (C::*)(int))&C::F) // Note missing staticmethod call!
.def("F", (void (C::*)(double))&C::F).staticmethod("F")
;
Run Code Online (Sandbox Code Playgroud) 我有一个方法(或函数),它返回对多态对象列表的引用:
class A {
};
class B : public A {
};
std::list<boost::shared_ptr<A> >& getList();
Run Code Online (Sandbox Code Playgroud)
我如何在boost :: python中公开这样的函数,以便在python中迭代列表时,我会看到不同类型的As和Bs?
我正在尝试使用boost :: python将Python类移植到C++,希望加快Python应用程序的执行(我移植到C++的类负责约30%的应用程序执行时间).
原始Python类的init看起来像:
class PyClass(object):
def __init__(self, child):
child.set_parent(self)
...
Run Code Online (Sandbox Code Playgroud)
我如何在C++构造函数中复制它?
如果我有一个C++类:
class CClass
{
// to get input args that match the Python class I need
CClass(boost::python::object &child)
{
// but how do I get the boost::python::object self
// as I only have *this in C++ ?
CClass& c = boost::python::extract<CClass&>(child);
c.set_parent(self);
}
...
}
Run Code Online (Sandbox Code Playgroud)
谢谢,马克
我在理解如何使用bjam指定和调用目标时遇到问题.通过这个,我的意思是我想为bjam提供命令行目标来构建(实际上来自Makefile),这些目标对应于构建过程的不同方面,而不仅仅是运行整个事物.
例如,现在当我键入'bjam'时,它会关闭并构建一个python扩展,运行一个单元测试文件,并创建一个单独的'main'可执行文件.我有自定义规则执行每一步,我的Jamfile只是按顺序列出它们:
project-name = example ;
sources =
$(project-name).cpp
$(project-name)_ext.cpp
;
build-ext $(project-name) : $(sources) ;
build-main $(project-name) ;
Run Code Online (Sandbox Code Playgroud)
在我的Jamroot(上一个目录)中我定义了这些规则,这里是不完整的文件:
# A rule to simplify declaration of extension tests:
rule run-test ( test-name : sources + )
{
import testing ;
testing.make-test run-pyd : $(sources) : : $(test-name) ;
}
# A rule to further simply declaration of extension tests:
rule run-ext-test ( project-name )
{
run-test $(project-name) : $(project-name)_ext test_$(project-name)_ext.py ;
}
# A rule to simplify …Run Code Online (Sandbox Code Playgroud) 所以我正在开发一个小项目,我在其中使用Python作为嵌入式脚本引擎.到目前为止,我使用boost.python并没有遇到太多麻烦,但是如果可能的话,我还有一些事情要做.
基本上,Python可以通过向类添加函数甚至数据值来扩展我的C++类.我希望能够在C++方面保持这些,所以一个python函数可以将数据成员添加到类中,然后传递给另一个函数的同一个实例仍将拥有它们.这里的目标是用C++编写通用核心引擎,让用户以他们需要的任何方式在Python中扩展它,而不必触及C++.
所以我认为可行的是我将一个boost::python::objectC++类存储为一个值self,当从C++调用python时,我会发送该python对象boost::python::ptr(),以便python端的修改将持续回到C++类.不幸的是,当我尝试这个时,我收到以下错误:
TypeError: No to_python (by-value) converter found for C++ type: boost::python::api::object
有没有办法将一个对象直接传递给这样的python函数,或者我可以通过任何其他方式来实现我想要的结果?
在此先感谢您的帮助.:)
在使用Boost.Python和C++时,有时我们会创建使用类本身和boost::shared_ptr<>版本绑定的类.由于许多原因,这非常方便,可以在很多地方使用.但是,当boost::python返回一个boost::shared_ptr<>在Python中生成并记录在C++静态变量上的值时,该机制似乎无法可靠地工作.
通常情况下,我希望返回的人会有boost::shared_ptr<>一个特殊的删除器来处理这个问题,但事实似乎并非如此.似乎发生的事情是返回的boost::shared_ptr只是包含一个指向Python中生成的值的指针,没有任何特殊考虑删除.这会导致双重删除(一个来自Python解释器本身,一个来自C++静态)的一致崩溃 - 或者至少看起来像它.
要使用下面的代码重现此行为,请创建如下所示的test.cc文件并使用以下脚本进行测试.
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
struct A {
std::string _a;
A(std::string a): _a(a) {}
std::string str() { return _a; }
};
static boost::shared_ptr<A> holder(new A("foo"));
static void set_holder(boost::shared_ptr<A> a_ptr) {
holder = a_ptr;
}
static boost::shared_ptr<A> get_holder() {
return holder;
}
BOOST_PYTHON_MODULE(test)
{
using namespace boost::python;
class_<A, boost::shared_ptr<A> >("A", init<std::string>())
.def("__str__", &A::str)
;
def("set_holder", &set_holder);
def("get_holder", &get_holder);
}
Run Code Online (Sandbox Code Playgroud)
使用以下Python测试程序:
import test
print(str(test.get_holder())) …Run Code Online (Sandbox Code Playgroud) 在python中调用C++函数时遇到一个奇怪的问题.
我公开了一个我想要调用函数的类:
class_<MyClass, std::shared_ptr<MyClass>>("MyClass", init<>())
// ...
.def("someFunc", &MyClass::someFunc)
;
Run Code Online (Sandbox Code Playgroud)
我std::shared_ptr<MyClass>从另一个通过公开的类中获取一个成员变量.def_readonly(...)
当我尝试调用该函数时,我收到以下错误:
File "pytest.py", line 27, in test_func
cu.someFunc("string")
Boost.Python.ArgumentError: Python argument types in
MyClass.someFunc(MyClass, str)
did not match C++ signature:
result(MyClass{lvalue}, std::string)
Run Code Online (Sandbox Code Playgroud)
据我所知,签名确实匹配.有人看到了这个问题吗?
我已经使用我能找到的每个boost python资源以及我能想到的每个安装和构建排列来解决这个问题已有40个小时了,但找不到对我有用的任何东西。
像我之前的许多人一样,当我在boost/boost_1_60_0/libs/python/example/tutorial目录中运行bjam时,出现链接器错误。我有Visual Studio 2015,并且尝试通过user-config.jam位于主目录中的文件的不同配置在32位Anaconda,64位Anaconda和纯Python 2.7上运行bjam 。
这是bjam使用任何设置运行后的构建日志。
Performing configuration checks
- 32-bit : yes (cached)
- arm : no (cached)
- mips1 : no (cached)
- power : no (cached)
- sparc : no (cached)
- x86 : yes (cached)
...found 7 targets...
- symlinks supported : yes
...found 1 target...
...updating 1 target...
config-cache.write bin\project-cache.jam
...updated 1 target...
...patience...
...patience...
...found 1917 targets...
...updating 9 targets...
msvc.link.dll ..\..\..\..\bin.v2\libs\python\build\msvc-14.0\debug\threading-multi\boost_python-vc140-mt-gd-1_60.dll
Creating library ..\..\..\..\bin.v2\libs\python\build\msvc-14.0\debug\threading-multi\boost_python-vc140-mt-gd-1_60.lib and …Run Code Online (Sandbox Code Playgroud) 有没有办法包装boost::optional<T>类型对象以通过公开它boost::python::class_(用于BOOST_PYTHON_MODULE)
struct Foo
{
boost::optional<int> bar;
};
BOOST_PYTHON_MODULE(module_name)
{
class_<Foo>("Foo")
.def_readwrite("bar", &Foo::bar);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我在Python中期望的是AttributeError
import module_name
f = module_name.Foo()
print f.bar
Run Code Online (Sandbox Code Playgroud)
因为bar尚未设置的值。而TypeError当
import module_name
f = module_name.Foo()
f.bar = "string"
Run Code Online (Sandbox Code Playgroud)
bar是int类型。
另一个相关的问题是以相同的方式导出boost::python::indexing_suite容器类型的类的对象。
使用boost::pythonapi 是否可以解决问题?
boost-python ×10
boost ×8
c++ ×7
python ×5
bjam ×2
boost-build ×1
c++11 ×1
lnk2001 ×1
lnk2019 ×1
makefile ×1
polymorphism ×1
shared-ptr ×1