参照如下博客可以看看写好的cpp 在G++中怎么编译:
http://blog.youkuaiyun.com/taiyang1987912/article/details/44779719
参考如下博客可以看看怎么在VS的项目中配置调用python接口
http://blog.youkuaiyun.com/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);
pName = PyString_FromString("pytest"); //载入名为 pytest的脚本
pModule = PyImport_Import(pName);

这篇博客详细介绍了如何在C++项目中调用Python接口。首先,提供了G++中编译C++代码的步骤,接着展示了在Visual Studio(VS)环境中配置Python接口的方法,特别针对VS中缺少Python27_d.lib的问题给出了解决方案。最后,给出了一个实例运行的C++代码片段。
最低0.47元/天 解锁文章
1222

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



