我正在使用boost.python来生成用c ++编写的python模块.我有一些基类与纯虚函数,我已导出如下:
class Base
{
virtual int getPosition() = 0;
};
boost::python::class_<Base>("Base")
.def("GetPosition", boost::python::pure_virtual(&Base::getPosition));
Run Code Online (Sandbox Code Playgroud)
在Python中我有代码:
class Test(Base):
def GetPosition(self):
return 404
Test obj
obj.GetPosition()
Run Code Online (Sandbox Code Playgroud)
RuntimeError:调用纯虚函数
怎么了?
使用boost python我需要创建嵌套的命名空间.
假设我有以下cpp类结构:
namespace a
{
class A{...}
namespace b
{
class B{...}
}
}
Run Code Online (Sandbox Code Playgroud)
明显的解决方案不起作用:
BOOST_PYTHON_MODULE( a ) {
boost::python::class_<a::A>("A")
...
;
BOOST_PYTHON_MODULE(b){
boost::python::class_<a::b::B>("B")
...
;
}
}
Run Code Online (Sandbox Code Playgroud)
它会导致编译时错误: linkage specification must be at global scope
有没有办法声明可以从Python访问的B类a.b.B?
我正在使用Boost Python来包装我创建的一些C++函数.我的一个C++函数包含22个参数.当我尝试用这个函数编译我的解决方案时Boost抱怨,我试图弄清楚它是否只是因为这个函数有太多的参数.
有谁知道这样的限制是否存在?我已经复制了我在下面的错误,而不是代码,因为我认为某人要么知道答案还是没有 - 如果没有限制,那么我只是试着弄清楚自己.首先十分感谢!
这是我收到的错误消息开头的副本...
1>main.cpp
1>c:\cpp_ext\boost\boost_1_47\boost\python\make_function.hpp(76): error C2780: 'boost::mpl::vector17<RT,most_derived<Target,ClassT>::type&,T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14> boost::python::detail::get_signature(RT (__thiscall ClassT::* )(T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14) volatile const,Target *)' : expects 2 arguments - 1 provided
1>c:\cpp_ext\boost\boost_1_47\boost\python\signature.hpp(236) : see declaration of 'boost::python::detail::get_signature'
Run Code Online (Sandbox Code Playgroud)
最终我得到了大约一百个与此类似的错误消息:
1>c:\cpp_ext\boost\boost_1_47\boost\python\make_function.hpp(76): error C2784: 'boost::mpl::vector17<RT,ClassT&,T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14> boost::python::detail::get_signature(RT (__thiscall ClassT::* )(T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14) volatile const)' : could not deduce template argument for 'RT (__thiscall ClassT::* )(T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14) volatile const' from 'std::string (__cdecl *)(const std::string &,jal::date::JULIAN_DATE,const std::string &,const std::string &,int,const std::string &,const std::string &,const std::string &,const std::string &,const std::string &,const std::string &,int,const std::string &,const std::string …Run Code Online (Sandbox Code Playgroud) 我想在c ++ app中使用embed python并调用python脚本中定义的函数.函数的参数是一个c ++对象.看我的代码:
class Test
{
public:
void f()
{
std::cout<<"sss"<<std::endl;
}
};
int main()
{
Py_Initialize();
boost::python::object main = boost::python::import("__main__");
boost::python::object global(main.attr("__dict__"));
boost::python::object result = boost::python::exec_file("E:\\python2.py", global, global);
boost::python::object foo = global["foo"];
if(!foo.is_none())
{
boost::python::object pyo(boost::shared_ptr<Test>(new Test())); // compile error
foo(pyo);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
python2.py:
def foo(o):
o.f()
Run Code Online (Sandbox Code Playgroud)
如何将c ++对象传递给foo?我知道swig可以做到这一点,但是boost :: python?
我使用Boost.Python包装了一个C++类.这些对象在C++方面具有强引用(boost :: shared_ptr),并且Python中也可能存在间歇性强引用.到目前为止,一切运作良好.但是,如果我从其中一个强引用创建python弱引用,则只要最后一个python强引用消失,就会删除此弱引用.我希望这个弱引用能够保持活着,直到C++方面的最后一个强引用也消失了.是否有可能实现这一目标?
换句话说:如果特定的C++对象(由Boost.Python包装)仍然存在,有没有办法从python中找到?
我正在使用外部程序来计算用C++编写并与python通过接口的矩阵boost::python.我想把这个C数组传递给numpy,根据作者的说法,这个能力已经用numpy实现了obj.__array_interface__.如果我在python脚本中调用它并将C++对象分配给X我,我将获得以下内容:
print X
#<sprint.Matrix object at 0x107c5c320>
print X.__array_interface__
#<bound method Matrix.__array_interface__ of <sprint.Matrix object at 0x107c5c320>>
print X.__array_interface__()
#{'shape': (5, 5), 'data': (4416696960, True), 'typestr': '<f8'}
print np.array(X)
#Traceback (most recent call last):
# File "<string>", line 96, in <module>
#ValueError: Invalid __array_interface__ value, must be a dict
Run Code Online (Sandbox Code Playgroud)
从我有限的理解,我相信问题X.__array_interface__是没有实际上没有返回任何东西().有没有办法np.array明确地传递这些参数或解决此问题.
我真的很擅长混合C++和python,如果这没有意义,或者我需要在任何部分阐述让我知道!
我正在尝试使用boost :: python库在C++中为Python3创建一个helloWorld模块.
这是一个CmakeList.txt:
set(Python_ADDITIONAL_VERSIONS 3.4)
find_package( PythonLibs 3.4 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )
find_package( Boost 1.56.0 EXACT COMPONENTS python3 REQUIRED )
include_directories( ${Boost_INCLUDE_DIR} )
# Define the wrapper library that wraps our library
add_library( hello SHARED main.cpp )
target_link_libraries( hello ${Boost_LIBRARIES} ${PythonLibs_LIBRARIES} )
# don't prepend wrapper library name with lib
set_target_properties( hello PROPERTIES PREFIX "" OUTPUT_NAME hello)
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <boost/python.hpp>
char const* greet( )
{
return "Hello world";
}
BOOST_PYTHON_MODULE(mymodule)
{
using namespace boost::python;
def( "greet", …Run Code Online (Sandbox Code Playgroud) 我有一个Boost Python对象
py::object obj = whatever();
Run Code Online (Sandbox Code Playgroud)
我想用普通的python规则打印它.
// I want the effect of print 'My object is ', obj
std::cout << "My object is " << obj << std::endl;
Run Code Online (Sandbox Code Playgroud)
这不会使用巨大的编译器转储进行编译.我该怎么做呢?
我成功地能够加载python脚本文件并boost::python在C++应用程序中调用函数.
在boost python EmbeddingPython wiki中,有一个关于如何加载python模块的提示.
namespace bp = boost::python;
bp::object import(const std::string& module, const std::string& path, bp::object& globals)
{
bp::dict locals;
locals["module_name"] = module;
locals["path"] = path;
bp::exec("import imp\n"
"new_module = imp.load_module(module_name, open(path), path, ('py', 'U', imp.PY_SOURCE))\n",
globals,
locals);
return locals["new_module"];
}
Run Code Online (Sandbox Code Playgroud)
我可以成功使用它来导入python模块(test.py)
int main()
{
Py_Initialize();
bp::object main = bp::import("__main__");
bp::object globals = main.attr("__dict__");
bp::object module = import("test", "test.py", globals);
bp::object run = module.attr("run");
run();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用hello-world test.py脚本运行上面的代码可以正常工作:
test.py: …
我都boost和boost-python安装通过brew我的MacOS 10.12塞拉利昂.但是,boost-python似乎并未认为其依赖项boost已安装:
$ brew info boost
boost: stable 1.68.0 (bottled), HEAD
Collection of portable C++ source libraries
https://www.boost.org/
/usr/local/Cellar/boost/1.65.1 (12,679 files, 401.2MB)
Poured from bottle on 2017-10-28 at 09:19:27
/usr/local/Cellar/boost/1.67.0_1 (13,506 files, 447.7MB) *
Poured from bottle on 2018-05-18 at 03:58:16
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/boost.rb
==> Dependencies
Optional: icu4c ?
==> Options
--with-icu4c
Build regexp engine with icu support
--without-single
Disable building single-threading variant
--without-static
Disable building static library variant
--HEAD
Install …Run Code Online (Sandbox Code Playgroud) boost-python ×10
c++ ×7
python ×7
boost ×6
cmake ×1
homebrew ×1
numpy ×1
package ×1
pure-virtual ×1
python-3.x ×1