参照如下博客可以看看写好的cpp 在G++中怎么编译:
http://blog.csdn.net/taiyang1987912/article/details/44779719
参考如下博客可以看看怎么在VS的项目中配置调用python接口
http://blog.csdn.net/c_cyoxi/article/details/23978007
参考如下地址可以解决VS中没有Python27_d.lib的问题,本人选择方法2:
http://blog.sina.com.cn/s/blog_75e9551f0101aajd.html
实例运行代码如下:
Cpp代码:
#include <Python.h>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
Py_Initialize(); //初始化 python
if (!Py_IsInitialized())
{
cout << "initialized error" << endl;
return -1;
}
PyRun_SimpleString("import sys"); // 执行 python 中的短语句
PyRun_SimpleString("print 'come in python'");
PyRun_SimpleString("sys.path.append('./')");
PyObject *pName(0), *pModule(0), *pDct(0), *pFunc(0), *pArgs(0);