Lua與C++的交互

在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;
}

運行結果如下
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章