现在的位置: 首页 > 综合 > 正文

C++嵌入Python最简例

2013年12月06日 ⁄ 综合 ⁄ 共 446字 ⁄ 字号 评论关闭

最简单的内嵌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
 

抱歉!评论已关闭.