我已经安装了Boost.Python.在安装过程中没有错误,但我有一个问题.当我尝试构建我的项目时,ererything是可以的,但是当我运行我的包装项目时出现错误:
import wrraped_project
ImportError: libboost_python.so.1.41.0: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我试图将项目复制到libboost_python.so.1.41.0以及lib到项目等.但每次我都有涂错误.
我收到一个编译错误,说该scoped_ptr私有的复制构造函数与以下代码片段:
class a {};
struct s
{
boost::scoped_ptr<a> p;
};
BOOST_PYTHON_MODULE( module )
{
class_<s>( "s" );
}
Run Code Online (Sandbox Code Playgroud)
此示例适用于shared_ptr.如果有人知道答案那就太好了.谢谢
Python解释器具有全局解释器锁,据我了解,扩展必须在多线程环境中获取它。但是Boost.Python HOWTO页面说扩展功能必须释放GIL并在退出时重新获取它。
我想抵制在这里进行猜测的诱惑,所以我想知道在以下情况下应该是什么GIL锁定模式:
Py_*函数。最后一个问题是,为什么链接文件说应该发布GIL并重新获得GIL?
我有一个类作为参数(二进制文件内容).
我想将python'str'类型转换为unsigned char的向量,但仅用于我的一个类方法.
BOOST_PYTHON_MODULE(hello) { class_<Hello>("Hello").
// This method takes a string as parameter and print it
.def("printChar", &Hello::printChar)
// This method takes a vector<unsigned char> parameter
.def("storeFile", &Hello::storeFile) }
Run Code Online (Sandbox Code Playgroud)
使用自定义转换器似乎是我需要的但是如果我修改我的boost :: python :: converter :: registry它将修改我对printChar的所有调用以及所有传递string的python方法将参数转换为vector.
如何注册每个方法的转换器?
我在使用Boost-Python为Python封装枚举时遇到问题。
最初,我打算在try-catch(我在下面插入了整个代码)语句中执行以下操作:
main_namespace["Motion"] = enum_<TestClass::Motion>("Motion")
.value("walk", TestClass::walk)
.value("bike", TestClass::bike)
;
Run Code Online (Sandbox Code Playgroud)
一切都很好,编译已经完成。在运行时,我收到此错误(对我来说这没有意义):
AttributeError: 'NoneType' object has no attribute 'Motion'
Run Code Online (Sandbox Code Playgroud)
之后,我决定在代码中使用BOOST_PYTHON_MODULE编写一个Python模块。初始化Python解释器后,我想立即使用此模块,但不知道how(?)。以下是我的整个代码:
#include <boost/python.hpp>
#include <iostream>
using namespace std;
using namespace boost::python;
BOOST_PYTHON_MODULE(test)
{
enum_<TestClass::Motion>("Motion")
.value("walk", TestClass::walk)
.value("bike", TestClass::bike)
;
}
int main()
{
Py_Initialize();
try
{
object pyMainModule = import("__main__");
object main_namespace = pyMainModule.attr("__dict__");
//What previously I intended to do
//main_namespace["Motion"] = enum_<TestClass::Motion>("Motion")
// .value("walk", TestClass::walk)
// .value("bike", TestClass::bike)
//;
//I want to use my enum here
//I need something …Run Code Online (Sandbox Code Playgroud) 我编写了一个C++方法,我需要将结构返回给Python.我已经能够mat使用BOOST按照此链接中描述的方法将OpenCV 从Python 发送到C++ .
现在我需要走另一条路; 从C++返回到Python,然后在Python中访问该结构.可以吗?
任何样本或参考链接都是好的.我在发布这个问题之前尝试了谷歌搜索,我无法获得任何样本或解释链接.
我正在尝试在官方网站上使用示例测试Boost python。但这会导致很多错误...以下是我的工作和错误。
Eclipse库搜索路径添加到“ usr / includ”(将目录放在此处)-lpython2.7(已安装Python2.7)usr/include/python2.7(在我的第一次尝试中,发生了错误:找不到pyconfig.h)这是我所做的测试代码,并且出错,该测试代码只是为了查看它是否可以使用boost python正常编译。
#include <iostream>
#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
int main(){
std::cout << "aaa" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
。在Eclipse中用红线表示的错误部分是BOOST_PYTHON_MODULE(hello_ext)
**** Build of configuration Debug for project tsetBoost ****
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/include/python2.7 -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -MMD -MP -MF"main.d" …Run Code Online (Sandbox Code Playgroud) Boost.Python v1.56中的以下示例显示了如何将Python 3.4.2解释器嵌入到您自己的应用程序中.不幸的是,在Windows 8.1下使用MSVC2013进行配置时,该示例无法正常使用.我还没有找到一个关于嵌入的完整示例,至少没有一个比10年左右更年轻.
运行它时收到以下错误:ImportError:'embedded_hello'不是内置模块
代码在这里:http://pastebin.com/shTtdxT8
任何暗示我可以做什么让这个运行?一般来说如何在Python中公开c ++类,反之亦然?
我boost::python在这个小小的mwe中使用了这个库.
#include <deque>
#include <boost/python.hpp>
typedef std::deque<long unsigned int> DequeUInt64;
BOOST_PYTHON_MODULE_INIT(tmp) {
boost::python::class_<DequeUInt64>("DequeUInt64")
.def("push_back" ,&DequeUInt64::push_back)
.def("push_front" ,&DequeUInt64::push_front);
}
Run Code Online (Sandbox Code Playgroud)
我观察到的代码与上面编译std=c++03和gnu++03,但不与c++11或c++0x.错误是:
tmp.cpp: In function 'void init_module_tmp()':
tmp.cpp:8:47: error: no matching function for call to 'boost::python::class_<std::deque<long unsigned int> >::def(const char [10], <unresolved overloaded function type>)'
.def("push_back" ,&DequeUInt64::push_back)
^
In file included [from /opt/local/include/boost/python.hpp:18:0], [from tmp.cpp:2]:
/opt/local/include/boost/python/class.hpp:223:11:
note: candidate:
template<class Derived> boost::python::class_<T, X1, X2, X3>::self&
boost::python::class_<T, X1, X2, X3>::def(const boost::python::def_visitor<Derived>&)
[with Derived = Derived; …Run Code Online (Sandbox Code Playgroud) 我一直在进行一个项目,在该项目中我想删除boost依赖项并将其替换为Python C API。
我花了一些时间了解Python C API,并且看到了
catch (error_already_set const &)
我阅读了boost文档,但是它解释了它的使用位置。但是我想知道为什么需要它,以及如何使用本机Python C api实现相同的功能。
boost-python ×10
c++ ×8
boost ×6
python ×6
binding ×1
c++11 ×1
eclipse ×1
msvc12 ×1
python-c-api ×1
wrapper ×1