python与C++相互使用时的连接问题,渴望大虾们给点帮助!

youxuwu 2003-10-18 07:47:59
首先,我写了一个游戏的剧情函数的DLL,我用了Boost.python.代码如下:
// gut.hpp
#ifndef GUT_HPP
#define GUT_HPP
#include "stdafx.h"

extern int Geti();
extern float Getf();
extern const char* Getc();
#endif

// gut.cpp
#include "stdafx.h"
#include "gut.h"

int Geti()
{
return 10;
}

float Getf()
{
return 3.245f;
}

const char* Getc()
{
return "test";
}

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(GutDll)
{
def("Geti", Geti);
def("Getf", Getf);
def("Getc", Getc);
}

我这个文件编译成GutDll.dll和GutDll.lib,我把这个DLL和lib放到我的python的
安装的目录下,GutDll放到C:\python22\DLLs下,GutDll.lib放到C:\python22\libs
下。

我在python22的IDLE(python GUI)中编写如下的程序:
// test2.py(test2是这个python的文件名,我下面的程序会使用)
import GutDll
gd = GutDll
print "test string is", gd.Getc()
print "test integet is", gd.Geti()
print "test float is", gd.Getf()

这个脚本在python中运行的很好,都是正确的。

为了让这个脚本在游戏中使用,我写了如下的程序在C++中运行python的程序。
// PythonWrapper.hpp
#ifndef PYTHONWRAPPER_HPP
#define PYTHONWRAPPER_HPP
#include "StdAfx.h"

#define SCRIPT_BEGIN namespace script{
#define SCRIPT_END }

#ifdef SCRIPT_EXPORTS
#define ST_API __declspec(dllexport)
#else
#define ST_API __declspec(dllimport)
#endif

#include "Py_Include\Python.h"

SCRIPT_BEGIN

class ST_API InterpreterScript
{
public:
InterpreterScript();
~InterpreterScript();
bool Initialize();
bool IsInitialized()const;
// execluate Python's string command
bool RunCommand(char* command)const;
// execluate a Python's file content
bool RunScript(const char* filename)const;
bool Finalize()const;
};

SCRIPT_END

#endif
##################################################################

// PythonWrapper.cpp
#include "StdAfx.h"
#include "PythonWrapper.h"

SCRIPT_BEGIN
InterpreterScript::
InterpreterScript()
{
}

InterpreterScript::
~InterpreterScript()
{
}

bool InterpreterScript::
Initialize()
{
Py_SetProgramName("Game script -- Python" );
Py_Initialize();
return true;
}

bool InterpreterScript::
IsInitialized()const
{
return Py_IsInitialized() == 0 ? false : true;
}

bool InterpreterScript::
RunCommand(char* command)const
{
int iRet = PyRun_SimpleString(command);
if (iRet<0)
return false;

return true;
}

bool InterpreterScript::
RunScript(const char* filename)const
{
FILE * fp;
fp = fopen(filename, "r" );
if(fp == NULL)
{
printf( "can't open file\n" ) ;
return false;
}
fclose(fp);

char* exec_str = NULL;
exec_str = (char*)malloc(strlen(filename) + 13);
sprintf(exec_str, "execfile('%s')", filename);
int success = 0;
success = PyRun_SimpleString(exec_str);
free(exec_str);
if (success == -1)
{
printf("Problem running script!\n");
return false;
}

return true;
}

bool InterpreterScript::
Finalize()const
{
Py_Finalize();
return true;
}

SCRIPT_END

同样的我把这个程序编译成Script.dll和script.lib。

我写了如下的一个测试程序,我把script.dll和script.lib,GutDll.dll和GutDll.lib都放到这个测试程序的当前的工作目录下。我把PythonWrapper.hpp也
放到同样的工作目录下。这个程序还需要使用python22_d.dll和python22_d.lib
和Boost_Python_debug.dll(这个程序都是在debug的版本下。)
测试程序如下:
#include "stdafx.h"
#include "PythonWrapper.h"
using namespace script;

#include <python.h>
#pragma comment(lib, "Script.lib")
int main(int argc, char* argv[])
{
InterpreterScript is;
is.Initialize();
bool b = is.IsInitialized();
is.RunScript("test2.py");
is.Finalize();
return 0;
}

现在,当我运行这个程序时,报如下的错误:
run it, has a wrong as:
Traceback (most recent call last):
File "<string>", line 1, in ?
ImportError: No module named GutDll.

PS: 如果test2.py没有使用GutDll.dll中的函数,只是一个纯python程序,例如
如下:
import math
print "cos(0.0) = ", msth.cos(0.0)
上面的程序就能成功的运行。

这个问题困扰我很久了,我一直都找不到解决方法,只要问问大家了,渴望大家能
给我一点帮助,我想把这个问题解决掉。谢谢大家了。

...全文
53 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bowdom 2003-11-21
  • 打赏
  • 举报
回复
晕了 感觉是路径不对 找不到文件
seawolfzxw 2003-11-19
  • 打赏
  • 举报
回复
我正在学习python帮你顶
mcsessx 2003-10-30
  • 打赏
  • 举报
回复
建议你使用Swig来生成python和c的接口
albertlee 2003-10-27
  • 打赏
  • 举报
回复
mark
richincsdn2 2003-10-21
  • 打赏
  • 举报
回复
好人啊好人哪,俺正苦劳阿
youxuwu 2003-10-19
  • 打赏
  • 举报
回复
希望各位多棒我想想,这个问题一旦解决,我将它写成文章,提供那些需要的人这方面的人。
我会感谢大家的。
呵呵!快乐白兔,我为了把问题说清楚,就把源代码贴上来了。是有些长,请各位谅解拉。
klbt 2003-10-19
  • 打赏
  • 举报
回复
超长的问题,帮你顶。
xiaonian_3654 2003-10-18
  • 打赏
  • 举报
回复
python扩展真的很烦人,编译选项要对,版本还要对,
还要把对象引用计数器搞对,
总之,很烦人

37,719

社区成员

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

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