最简单的内嵌Python例子,完全照搬手册上的例子:
#include <Python.h>
int
main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime/n"
"print 'Today is',ctime(time())/n");
Py_Finalize();
return 0;
}
注意:
* 需要指定include, lib目录
* 对于g++, 需手工加入python库: -lpython24
* 对于VC, 库会自动加入,但应绕过调试库:
// http://mail.python.org/pipermail/python-list/2002-February/089443.html
#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif
本文提供了一个简单的内嵌Python程序示例,展示了如何使用C++调用Python代码执行时间显示任务。介绍了必要的编译配置步骤,包括指定include及lib目录,并针对不同编译器给出了配置建议。
482

被折叠的 条评论
为什么被折叠?



