c和python混编,c调用python模块中的函数
2011-12-13 22:25
507 查看
Tool ---> Option ---> Projectc and Solutions ---> VC++ Directories
在Include files加入python安装目录的include文件所在路径
在Libraray files加入python安装目录的libs文件夹所在路径
进入python的include目录,查找pyconfig。
ifdef _DEBUG
# pragma comment(lib,"python27_d.lib")
# else
# pragma comment(lib,"python27.lib")
# endif /* _DEBUG */
把python27_d.lib修改为python27.lib
之后保存即可。
http://hi.baidu.com/zhengyl878/blog/item/bb1a9b3e6c2d97d37c1e713a.html这个网址有关于python C API的一些基本用法
在Include files加入python安装目录的include文件所在路径
在Libraray files加入python安装目录的libs文件夹所在路径
进入python的include目录,查找pyconfig。
ifdef _DEBUG
# pragma comment(lib,"python27_d.lib")
# else
# pragma comment(lib,"python27.lib")
# endif /* _DEBUG */
把python27_d.lib修改为python27.lib
之后保存即可。
#include <Python.h> using namespace std; void main() { //这样也可以!! //system("test.py"); //初始化python环境,为了之后使用python 提供的C API Py_Initialize(); PyObject *pName,*pModule,*pFunc,*pArgs,*pValue; if (!Py_IsInitialized()) { printf("Initialized Error! \n"); } //最简易的python C API //PyRun_SimpleString("print 'hello world'"); //得到python脚本的名字,并把这个模块引用进来,这个时候把模块主函数执行了!! pName = PyString_FromString("hello"); pModule = PyImport_Import(pName); if (!pModule) { printf("can not open the script! \n"); } //在引用的python模块中查找定义的函数 pFunc = PyObject_GetAttrString(pModule,"hello"); //第一种给函数参数赋值的方法 /*pArgs = PyTuple_New(1); pValue = PyString_FromString(" 10000"); PyTuple_SetItem(pArgs,0,pValue);*/ //第二种给函数参数赋值的方法 pArgs = Py_BuildValue("(i)",10); //调用这个函数,第一个参数为返回的函数,第二个参数为函数参数列表 PyObject_CallObject(pFunc,pArgs); //PyObject_CallObject(pFunc,NULL); //清理python环境 Py_Finalize(); //在这次写程序中遇到的问题就是:开始的时候c++工程名为test_project, //python脚本名称是test.py,所以在引用模块的时候错误地使用了其他文件 //推测这个和python应用模块时候查找文件有关把,之后把脚本改名之后 //就都可以了,弄了将近两个小时,很汗啊!
}
import os import sys def hello(a): #print "hello world!" print "execute the func"+str(a) os.mkdir("test") # system("mkdir test") print "hello world" print "+++++++++++++++++++"
上面为hello.py
http://hi.baidu.com/zhengyl878/blog/item/bb1a9b3e6c2d97d37c1e713a.html这个网址有关于python C API的一些基本用法
相关文章推荐
- c和python混编,c调用python模块中的函数
- Python库(3):引用多个模块,调用模块中的函数
- VS2008环境下C++调用Python模块函数----一封邮件引发的关机
- Python3基础 创建一个模块 导入并调用其中的函数 主代码与模块代码在同一文件夹下
- Python引用多个模块,调用模块中的函数时,要注意的地方
- PYTHON 调用EXCEL VBA 函数:使用win32com模块
- 导出 C++ 函数模块给 Python3 调用
- python调用模块&函数
- python只引入模块并不能直接调用该模块的函数
- Python 3.X 调用多线程C模块,并在C模块中回调python函数的示例
- [置顶] Python 模块里函数的调用方法和import语句的作用(适合初学者理解函数的调用)
- python中不同模块(函数、类、变量)的调用
- python 使用 signal模块实现函数调用超时问题
- Python 引用多个模块,调用模块中的函数时...要注意的地方
- Python 引用多个模块,调用模块中的函数时...要注意的地方
- Python 3.X 调用多线程C模块,并在C模块中回调python函数的示例
- C++调用python,直接调用模块内函数,调用类内函数
- Python 3.X 调用多线程C模块,并在C模块中回调python函数的示例
- 【Python模块】functools —— 作用于可调用对象的高阶函数集合
- python 调用自定义的模块函数