锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
{
Py_Initialize();
PyObject * pModule = NULL;//澹版槑鍙橀噺
PyObject * pFunc = NULL;// 澹版槑鍙橀噺
pModule =PyImport_ImportModule("helloworld");//榪欓噷鏄璋冪敤鐨勬枃浠跺悕
if (!pModule)
cout << "err";
handle<>* _module; // Module handle.
_module = new handle<>(pModule);
// 閫氳繃call_method
int i = call_method<int>(_module->get() , "Hello" , 4);
// 閫氳繃attr
main_module.attr("Hello")(2);
// main_module.attr("Hello")(2); 灞曞紑濡備笅
object main_module(*_module);
object FunObj = main_module.attr("Hello"); // 鑾峰彇hello鍑芥暟瀵硅薄
// 閫氳繃object鐖剁被鐨刼perator()鎿嶄綔絎?/span>
FunObj(1);
// 閫氳繃call
i = call<int>(FunObj.ptr(), 3);
{ // 閬嶅巻涓閬峱ython鍐呯疆綾誨瀷list瀵硅薄涓殑鍊?/span>
list List = call_method<list>(_module->get() , "getList");
int n = len(List);
for (int i = 0 ; i < n ; i++)
{
int val = extract<int> (List[i]); // 鑾峰彇List[i]浜х敓鐨刼bject瀵硅薄涓殑鍊?/span>錛岄氳繃extract妯℃澘鏉ヨ漿鎹?br /> cout << val << " ";
}
}
return 0;
}
]]>
浠ヤ笅鍙槸瀵硅嚜宸辨懜绱㈢粨鏋滅殑鎬葷粨錛屽叾涓毦鍏嶅瓨鍦ㄩ敊璇紝濡傛湁浠諱綍閿欒錛屾湜涓嶆儨鎸囨
綾誨畾涔夛細
{
private:
int m_val;
string m_nam;
public:
void setVal(int val)
{
m_val = val;
}
int getVal()
{
return m_val;
}
void setName(const char *name)
{
m_nam = name;
}
const char* getName()
{
return m_nam.c_str();
}
void show()
{
std::cout <<"hello world" <<std::endl;
}
};
鏂規硶涓錛?/strong>
{
Py_Initialize();
PyObject *pModule = PyImport_ImportModule("helloworld");
handle<>_module(pModule);
object main_module(_module);
// 瀵煎嚭C++綾誨埌python涓?/span>
main_module.attr("Demo") = class_ <Demo>("Demo") // 閫氳繃boost::python涓殑class_灝咰++綾誨寘瑁呮垚object綾誨瀷錛屽洜涓簆ython涓墍鏈夌被鍨嬮兘鏄痮bject
.def("setVal", &Demo::setVal)
.def("setName", &Demo::setName)
;
// 閫氳繃boost::python鎻愪緵鐨勬ā鏉垮嚱鏁皃tr錛屾潵灝咲emo瀵硅薄浼犲叆python
Demo a;
main_module.attr("test") = object(ptr(&a));
std::cout <<"涔嬪墠:"<< a.getName() << " " << a.getVal()<<endl;
call_method<void>(pModule , "fun2");
std::cout <<"涔嬪悗:"<< a.getName() << " " << a.getVal()<<endl;
Demo tmp = call_method<Demo>(pModule , "ReturnCppClass");
std::cout << tmp.getName() << " " << tmp.getVal() <<endl;
helloworld.py鑴氭湰鍐呭錛?br />
tmp = test;
tmp.setVal(12345);
tmp.setName("2011-5-19");
tmp = Demo();
tmp.setName('123456789')
tmp.setVal(99);
return tmp;
鏂規硶浜岋細
閫氳繃鏅鴻兘鎸囬拡瀹炵幇C++瀵硅薄鐨勫鍏?strong>
void fun2()
{
Py_Initialize();
Demo_ptr spD1(new Demo);
spD1->setName("NULL");
spD1->setVal(0);
try
{
PyObject *pModule = PyImport_ImportModule("helloworld");
register_ptr_to_python <Demo_ptr>(); // 娉ㄥ唽鍙兘鎸囬拡
handle<>* _module;
_module = new handle<>(pModule);
object main_module(*_module);
object dictionary = main_module.attr("__dict__");
dictionary["Demo"] = class_ <Demo>("Demo")
.def("setVal", &Demo::setVal)
.def("setName", &Demo::setName)
;
main_module.attr("DemoObj") = spD1; // 娣誨姞鏅鴻兘鎸囬拡鍒拌剼鏈紝騫墮噸鍛藉悕
call_method<void>(pModule , "SetObj"); // 鍦ㄨ剼鏈腑閫氳繃鎿嶇旱鏅鴻兘鎸囬拡鏉ユ帶鍒跺璞?/span>
std::cout << a.getName() << " " << a.getVal()<<endl;
}
catch (error_already_set)
{
PyErr_Print();
}
std::cout << "spD1->getName(): " << spD1->getName() <<std::endl;
std::cout << "spD1->getVal(): " << spD1->getVal() <<std::endl;
}
鑴氭湰鍐呭錛?br />
DemoObj.setVal(100);
DemoObj.setName('hello');
1.register_ptr_to_python <Demo_ptr>(); 娌℃湁榪欏彞榪愯鏃朵細寰楀埌閿欒銆傚叧浜巖egister_ptr_to_python錛宐oost涓殑瑙i噴鏄細
“a function template which registers a conversion for smart pointers to Python”
2.
.def("setVal", &Demo::setVal)
.def("setName", &Demo::setName)
;
.def("setVal", &Demo::setVal)
.def("setName", &Demo::setName)
;
-----------------------------------------------------------------------------------
浠ヤ笂灝辨槸鍏充簬濡備綍灝咰++瀵硅薄浼犲叆python鐨勪竴鐐逛範寰楋紝濡傛湁浠諱綍閿欒錛屾湜涓嶆儨鎸囨
-----------------------------------------------------------------------------------
P.S.: cppblog鎻愪緵鐨勭紪杈戝櫒涓嶅ソ鐢?img src ="http://m.shnenglu.com/zhaoyg/aggbug/147885.html" width = "1" height = "1" />
]]>
浜庢槸閫氳繃鍏抽敭瀛?#8220;C++宓屽叆python 鍙戝竷”錛屾壘鍒頒簡涓涓敤py2exe鏉ヨВ鍐抽棶棰橈紝浣嗘垜璇曚簡鍗婂ぉ閮芥病鎴愬姛錛堜篃璁告槸鎴戣嚜宸辨病寮勫鐨勫師鍥狅紝鍥犱負浜哄閮芥垚鍔熶簡錛夈?br />鍐嶅悗鏉ユ垜鎶婂叧閿瓧鏀逛負浜?#8220;python C++ distribute embed”錛堝嵆鑻辨枃鍏抽敭瀛楋級錛岃繖涓嬫嫧浜戣澶╀簡錛屽洜涓哄湪Google鐨勭涓欏靛氨鏈変竴涓鍥藉笘瀛愬叾鏍囬姝e拰鎴戞剰錛堟灉鐒惰嫳璇粰鍔涳級銆?br />
鐪嬩簡閭d釜璐村瓙鍙戠幇錛屼箣鎵浠ヤ笉鑳藉湪娌℃湁瀹夎python鐨勬満瀛愪笂榪愯鏄洜涓猴細
“
In addition to pythonxy.dll, you also need the entire Python library, i.e. the contents of the lib folder, plus the extension modules, i.e. the contents of the DLLs folder. Without the standard library, Python won't even start, since it tries to find os.py (in 3.x; string.py in 2.x). On startup, it imports a number of modules, in particular site.py.
”
鍘熸枃錛?a >http://stackoverflow.com/questions/1387906/c-with-python-embedding-crash-if-python-not-installed
]]>