[英]Boost python, calling c++ functions from python when embedded
I currently have the following: 我目前有以下内容:
namespace py=boost::python;
//C++
void f() {
std::cout << "hello world\n";
}
//I am not precious about this, if it can be done without a module that would be great
BOOST_PYTHON_MODULE(test)
{
py::def("f", f);
}
int main() {
auto main_module =py::import("__main__");
auto main_namespace =main_module.attr("__dict__");
//???????
auto result=py::exec_file("t.py", main_namespace);
}
//t.py
f()
I am trying to call f, but I am not sure of the glue required to get it to work. 我试着打电话给f,但我不确定要让它工作所需的粘合剂。 With classes I can do
我可以上课
int main() {
//...
py::obejct p_my_type=py::class_<my_type>("my_type").def("f", &my_type::f);
main_namespace["my_type"]=p_my_type;
//...
however boost::python::def
doesn't appear to return a boost::python::object
like class_
does 但是
boost::python::def
似乎没有像class_
那样返回一个boost::python::object
My questions are, how do I get the first test case to work as expected? 我的问题是,如何让第一个测试用例按预期工作? And secondly is the way in which I am exposing my types in the second code snippet "correct"?
其次是我在第二个代码片段“正确”中公开我的类型的方式?
The fix was simple but wasn't mentioned in the doc on this page: 修复很简单,但在本页的文档中没有提到:
http://www.boost.org/doc/libs/1_55_0/libs/python/doc/tutorial/doc/html/python/embedding.html http://www.boost.org/doc/libs/1_55_0/libs/python/doc/tutorial/doc/html/python/embedding.html
I needed to do this: 我需要这样做:
auto main_module =py::import("__main__");
auto main_namespace =main_module.attr("__dict__");
inittest();
auto result=py::exec_file("t.py", main_namespace);
from test import f
f()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.