c++调用python完整代码(开发环境vs2008)
2010-03-27 10:43
661 查看
// python_c++_test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//#include "Python_h/Python.h"
/************************************************************************/
/* 运行函数
/* PyAPI_FUNC(int) PyRun_AnyFileFlags(FILE *, const char *, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_AnyFileExFlags(FILE *, const char *, int, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_SimpleFileExFlags(FILE *, const char *, int, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_InteractiveOneFlags(FILE *, const char *, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_InteractiveLoopFlags(FILE *, const char *, PyCompilerFlags *); */
/************************************************************************/
#ifdef WIN32
#include "Python_h/Python.h" //windows平台下
#else
#include "/usr/src/Python-2.4/Include/Python.h" //linux平台下
#endif
int _tmain(int argc, _TCHAR* argv[])
{
PyObject *pName,*pModule,*pDict,*pFunc,*pArgs,*pValue;
Py_Initialize();
//设置环境变量
#ifdef WIN32
PyRun_SimpleString("import sys");//python执行字符串
PyRun_SimpleString("sys.path.append('./Python_script')");
// PyRun_SimpleString("import os");
// PyRun_SimpleString("import string");
#endif
//加载模块
pModule=PyImport_ImportModule("aaa");
//pArgs=PyObject_CallMethod(pModule,"max","ii",2,3);
//获取函数
pFunc=PyObject_GetAttrString(pModule,"max");
//调用函数
pArgs=PyObject_CallFunction(pFunc,"ii",10,80);
printf("%d",PyLong_AsLong(pArgs));
Py_Finalize();
/************************************************************************/
/* 以下是另一种加载python模块的方法
//pName=PyString_FromString("test");
//pModule=PyImport_Import(pName);
//pDict = PyModule_GetDict(pModule);
//pFunc = PyDict_GetItemString(pDict, "max");
////参数进栈
//pArgs=PyTuple_New(2);
////设置参数值
//PyTuple_SetItem(pArgs, 0, Py_BuildValue("i",10));
//PyTuple_SetItem(pArgs, 1, Py_BuildValue("i",15));
////调用函数
//pValue=PyObject_CallObject(pFunc, pArgs);
//查找函数
//pFunc = PyDict_ GetItemString_r(pDict, "max");
*/
/************************************************************************/
return 0;
}
//
#include "stdafx.h"
//#include "Python_h/Python.h"
/************************************************************************/
/* 运行函数
/* PyAPI_FUNC(int) PyRun_AnyFileFlags(FILE *, const char *, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_AnyFileExFlags(FILE *, const char *, int, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_SimpleFileExFlags(FILE *, const char *, int, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_InteractiveOneFlags(FILE *, const char *, PyCompilerFlags *);
/* PyAPI_FUNC(int) PyRun_InteractiveLoopFlags(FILE *, const char *, PyCompilerFlags *); */
/************************************************************************/
#ifdef WIN32
#include "Python_h/Python.h" //windows平台下
#else
#include "/usr/src/Python-2.4/Include/Python.h" //linux平台下
#endif
int _tmain(int argc, _TCHAR* argv[])
{
PyObject *pName,*pModule,*pDict,*pFunc,*pArgs,*pValue;
Py_Initialize();
//设置环境变量
#ifdef WIN32
PyRun_SimpleString("import sys");//python执行字符串
PyRun_SimpleString("sys.path.append('./Python_script')");
// PyRun_SimpleString("import os");
// PyRun_SimpleString("import string");
#endif
//加载模块
pModule=PyImport_ImportModule("aaa");
//pArgs=PyObject_CallMethod(pModule,"max","ii",2,3);
//获取函数
pFunc=PyObject_GetAttrString(pModule,"max");
//调用函数
pArgs=PyObject_CallFunction(pFunc,"ii",10,80);
printf("%d",PyLong_AsLong(pArgs));
Py_Finalize();
/************************************************************************/
/* 以下是另一种加载python模块的方法
//pName=PyString_FromString("test");
//pModule=PyImport_Import(pName);
//pDict = PyModule_GetDict(pModule);
//pFunc = PyDict_GetItemString(pDict, "max");
////参数进栈
//pArgs=PyTuple_New(2);
////设置参数值
//PyTuple_SetItem(pArgs, 0, Py_BuildValue("i",10));
//PyTuple_SetItem(pArgs, 1, Py_BuildValue("i",15));
////调用函数
//pValue=PyObject_CallObject(pFunc, pArgs);
//查找函数
//pFunc = PyDict_ GetItemString_r(pDict, "max");
*/
/************************************************************************/
return 0;
}
相关文章推荐
- C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用
- C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用
- C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用
- VS2008环境下C++调用Python模块函数----一封邮件引发的关机
- C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用
- C/C++代码静态检查工具PC-lint在VS2008开发环境中的安装配置和使用
- C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用
- C/C++代码静态检查工具PC-lint在VS2008开发环境中的安装配置和使用
- Linux下python如何调用c++的代码,用ctypes方法。。
- WIN10下 python+eclipse+jdk+PyDev(离线) 搭建java,python,C/C++开发环境
- Windows平台安装最新OpenCV-2.4.9,利用Eclipse、MinGW构建C++调用OpenCV开发环境
- 高效开发Python扩展模块方法:用SWIG封装C/C++代码
- C++调用Python环境
- Windows平台安装最新OpenCV-2.4.9,利用Eclipse、MinGW构建C++调用OpenCV开发环境
- 如何让python调用C和C++代码
- MyEclipse+MinGW+CDT+ADT+PyDev 搭建C/C++、Android与Python开发环境
- Qt之开发环境配置——在VS2008中为QT增加代码提示功能
- C++调用Lua编程环境搭建及测试代码示例
- windows和linux环境下java调用C++代码-JNI技术
- 使用SWIG实现Python调用C/C++代码