Boost python,在嵌入时从python调用c++函数

2024-04-26 21:28:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前有以下情况:

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()

我想打电话给f,但我不确定要用多少胶水才能使它工作。我可以上课

^{pr2}$

但是boost::python::def似乎不像class_那样返回boost::python::object

我的问题是,如何让第一个测试用例按预期工作? 第二,我在第二个代码片段中公开类型的方式是否“正确”?在


Tags: pyhelloworldautomaindef情况be