如何在VC项目中嵌入python?

uaiia 2004-03-18 04:25:37
拿到一个免费的共享代码,里面全是python文件,现在需要在vc中嵌入python的功能,不知如何实现?
...全文
514 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
oncsdn 2004-08-17
  • 打赏
  • 举报
回复
有点误,如下

#ifdef _DEBUG
#define D_E_B_U_G
#undef _DEBUG
#include <Python.h>
#else
#include <Python.h>
#endif
#ifdef D_E_B_U_G
#define _DEBUG
#undef D_E_B_U_G
#endif
oncsdn 2004-08-17
  • 打赏
  • 举报
回复
关于 lib 问题


1. 在VC中加入 C:\Python23\Lib 路径
2. 按如入方式头文件
#ifdef _DEBUG
#define D_E_B_U_G
#undef _DEBUG
#include <Python.h>
#endif
#ifdef D_E_B_U_G
#define _DEBUG
#undef D_E_B_U_G
#endif

参阅: http://community.csdn.net/Expert/topic/3037/3037303.xml?temp=.7859918
oncsdn 2004-08-17
  • 打赏
  • 举报
回复
好东西呀

还是 CSDN 好,俺在 google 上找了半天也没有找到


mark....................................
albertlee 2004-08-13
  • 打赏
  • 举报
回复
晕那 ,,,,,

原来是我自己写的东西, 还要mark , 看来我离开 CSDN 论坛的日子是长了点
albertlee 2004-08-13
  • 打赏
  • 举报
回复
我也来 mark 一个
功名半纸 2004-07-04
  • 打赏
  • 举报
回复
mark!!!!!!!!!!!!!!!!!!!
lodestar51 2004-06-26
  • 打赏
  • 举报
回复
学习........
howler 2004-06-18
  • 打赏
  • 举报
回复
mark
zzj0820 2004-06-11
  • 打赏
  • 举报
回复
mark
孙亖 2004-05-07
  • 打赏
  • 举报
回复
mark
albertlee 2004-04-01
  • 打赏
  • 举报
回复
1. 安装 Python 到 d:\python23
2. 新建一个VC项目, 在 Project Setting 中,把 d:\python\include 加入“其他头文件”哪一项,把 d:\python\libs 加入 link 的附加目录里。
3. 在主文件里这样写:

#include <python.h> // PYthon 头文件
#include <stdio.h>


void main(void)
{
Py_Initialize(); //python 解释器的初始化
if(!Py_IsInitialized())
{
printf("can't init python");
return;
}


PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");

PyObject *pName,*pModule,*pDict,*pFunc,*pArgs,*pRes;

pName = PyString_FromString("pytest"); // 这个就是你要嵌入的python 文件名

pModule = PyImport_Import(pName);
if(!pModule)
{
printf("can't find pytest.py");
getchar();
return ;

}

pDict = PyModule_GetDict(pModule); // 得到python模块中的函数表
if(!pDict)
return;

pFunc = PyDict_GetItemString(pDict,"add");
if(!pFunc || !PyCallable_Check(pFunc))
{
printf("can't find function [add]");
getchar();
return;
}

*pArgs;
// 添参数
pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs,0,Py_BuildValue("l",343));
PyTuple_SetItem(pArgs,1,Py_BuildValue("l",42356));

PyObject_CallObject(pFunc , pArgs); // 调用 python 程序


pFunc = PyDict_GetItemString(pDict,"str_find");
if(!pFunc || !PyCallable_Check(pFunc))
{
printf("can't find function [add]");
getchar();
return;
}

// 另外一个函数例子,演示了如何向 python 传递参数,和如何传回 C++
pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs,0,Py_BuildValue("s","abcdef"));
PyTuple_SetItem(pArgs,1,Py_BuildValue("s","de"));

long ret_val = PyInt_AsLong(PyObject_CallObject(pFunc , pArgs));

printf("%d\n",ret_val);


Py_DECREF(pModule);


Py_Finalize(); // 清除
getchar();
}

// -----------------------------------------
python 文件 pytest.py
def add(a,b):
print "in python function add"
print a+b
return

def str_find(str1,str2):
print "str1:",str1
print "str2:",str2
return str1.rfind(str2)
lodestar521 2004-04-01
  • 打赏
  • 举报
回复
学习。。。。。。。。。。。
xrenwu 2004-03-31
  • 打赏
  • 举报
回复
关注
iBear 2004-03-19
  • 打赏
  • 举报
回复
boost http://boost.sourceforge.net/
swig http://swig.sourceforge.net/

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧