C++与python的相互使用的连接问题,渴望大虾们给我一些指点!
首先,我写了一个游戏的剧情函数的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)
上面的程序就能成功的运行。
这个问题困扰我很久了,我一直都找不到解决方法,只要问问大家了,渴望大家能
给我一点帮助,我想把这个问题解决掉。谢谢大家了。