在VS2010环境下实现Lua与C++的交互
一:搭建环境
工程下载地址:https://github.com/lining91/Lua53
1:下载lua源代码:http://download.csdn.net/detail/lining0420/9858025
2:将代码解压。
3:创建一个win32工程Lua53(如下图所示)
选择静态库,不选择预编译头文件(如下图)
4:将解压的Lua源代码中的src目录下的代码加到Lua53中
编译生成Lua53.lib静态库
二:C++调Lua
工程下载地址:https://github.com/lining91/C2Lua
创建一个空白工程C2Lua
加载之前生成的Lua53.lib
* main.lua文件*
print("into C2Lua file")
teststr = "a test lua string"
tabletest = {id = 888, num = 666, test = "main.lua"}
function C2Lua_Add( x, y )
return x + y
end
main.cpp文件
#include <iostream>
#include <string.h>
using namespace std;
// 加载Lua53.lib
#pragma comment(lib, "..\\lib\\Lua53.lib")
extern "C"
{
#include "Lua/lua.h"
#include "Lua/lauxlib.h"
#include "Lua/lualib.h"
};
lua_State* L;
int C2Lua_Add( int nX, int nY )
{
int nSum;
lua_getglobal( L, "C2Lua_Add" );
lua_pushnumber( L, nX );
lua_pushnumber( L, nY );
int nresult = lua_pcall( L, 2, 1, 0 );
if ( nresult )
{
const char *pErrorMsg = lua_tostring(L, -1);
cout << pErrorMsg << endl;
lua_close(L);
return -1;
}
nSum = (int)lua_tonumber( L, -1 );
lua_pop( L, 1 );
return nSum;
}
void main( )
{
int nSum = 0;
L = luaL_newstate( );
luaopen_base( L );
luaL_openlibs( L );
// 加载lua文件
int nRet = luaL_loadfile(L, "..\\code\\C2Lua.lua");
if ( nRet )
{
cout << " load file error " << endl;
return ;
}
// 运行lua文件
nRet = lua_pcall( L, 0, 0, 0);
if ( nRet )
{
cout << "pcall error." << endl;
return;
}
// 调用函数
int nX = 5;
int nY = 8;
nSum = C2Lua_Add( nX, nY );
cout << nX << " + " << nY << " = " << nSum << endl;
// 读取变量
lua_getglobal( L, "teststr");
string str = lua_tostring( L, -1 );
cout << "teststr is : " << str.c_str() << endl;
lua_pop( L, 1 );
读取table
lua_getglobal( L, "tabletest" );
lua_getfield( L, -1, "id" );
str = lua_tostring( L, -1 );
cout << "tabletest id is : " << str.c_str() << endl;
lua_pop( L, 1 );
lua_getfield( L, -1, "num" );
str = lua_tostring( L, -1 );
cout << "tabletest num is : " << str.c_str() << endl;
system("pause");
return;
}
运行结果如下:
三:Lua调C++
Lua中以函数指针的形式调用函数。
工程下载地址:https://github.com/lining91/Lua2C
创建一个空白的工程Lua2C
加载之前生成的Lua53.lib
Lua2C.lua文件
local value1 = 10
local value2 = 30
sum = Lua2C_Add( value1, value2 )
print("Lua2C file :" .. value1 .. " + " .. value2 .. " = " .. sum)
main.cpp文件
#include <iostream>
#include <string.h>
using namespace std;
// 加载Lua53.lib
#pragma comment(lib, "..\\lib\\Lua53.lib")
extern "C"
{
#include "Lua/lua.h"
#include "Lua/lauxlib.h"
#include "Lua/lualib.h"
};
lua_State* L;
static int Lua2C_Add( lua_State* L )
{
// 返回栈中元素的个数
int nCount = lua_gettop( L );
int nNum = 0;
for ( int nIdx = 1; nIdx <= nCount; nIdx++ )
{
if ( !lua_isnumber( L, nIdx ) )
{
lua_pushstring( L, " Error" );
lua_error( L );
}
nNum += (int)lua_tonumber( L, nIdx );
}
lua_pushnumber( L, nNum );
return 1;
}
void main( )
{
int nSum = 0;
L = luaL_newstate( );
luaopen_base( L );
luaL_openlibs( L );
// 注册接口
lua_register( L, "Lua2C_Add", Lua2C_Add);
// 加载lua文件
int nRet = luaL_loadfile(L, "..\\code\\Lua2C.lua");
if ( nRet )
{
cout << " load file error " << endl;
return ;
}
// 运行lua文件
nRet = lua_pcall( L, 0, 0, 0);
if ( nRet )
{
cout << "pcall error." << endl;
return;
}
lua_getglobal( L, "sum" );
cout << " sum = " << lua_tonumber( L, -1) << endl;
lua_pop( L, 1);
lua_close( L );
system("pause");
return;
}
运行结果如下