以下是此示例的精简形式:http: //www.boost.org/doc/libs/1_51_0/libs/python/doc/v2/exec.html#examples
从 C++ 调用的 Python 函数,存储在文件script.py中:
def greet():
return 'Hello from Python!'
执行 Python 函数的 C++ 代码:
#include <iostream>
#include <string>
#include <boost/python.hpp>
using namespace boost::python;
void greet()
{
object main = import("__main__");
object global(main.attr("__dict__"));
object result = exec_file("script.py", global, global);
object greet = global["greet"];
std::string message = extract<std::string>(greet());
std::cout << message << std::endl;
}
我的问题是:我是否需要保持main、global和result对象活着才能调用greet?