以下は、この例の要約形式です。http ://www.boost.org/doc/libs/1_51_0/libs/python/doc/v2/exec.html#examples
ファイルscript.pyに保存されているC++から呼び出すPython関数:
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;
}
私の質問は、 greetを呼び出すことができるように、メインオブジェクト、グローバルオブジェクト、および結果オブジェクトを存続させる必要があるかどうかです。