AKever

导航

编译lualib.lib(cpp与lua交互第一步)

编译lualib.lib(cpp与lua交互第一步)

VS2012, lua5.3.0

====================================================

1. 下载lua源码

  http://www.lua.org/download.html

  解压后得到:doc src Makefile README (Makefile 文件应该是linux下编译文件)

2. 打开vs2012新建空白工程Test (默认win32 console)

3. 在解决方案中添加新的lib工程(WIN32->选择静态LIB 不要预编译头)lualib

右击解决方案 -> Add -> New Project -> (win32->next->Static libray->"去除勾选:Precomplied header" -> finish)

4. 配置lualib工程

a>   将lua源代码下的src文件拷入lualib文件夹下,并在工程中导入
b>   lualib工程属性C/C++ > Genaral > Additional Include Directories 添加lua源代码所在目录(我这是$(SolutionDir)lualib\src)   
c>   C/C++ > Advanced > Compile As 选择C编译器(Compile as C Code (/TC))

 

5. 编译lua静态库 (Release)

右击lualib 工程 -> build (此时,静态库已成, 会在 $(SolutionDir)\Release 目录下)

6. 配置Test工程属性

a>   在Test属性中配置VC++ Directories > Library Directories 添加第5步编译出的lualib.lib的路径
b>   在Test属性中配置C/C++ > Genaral > Additional Include Directories 添加lua源代码所在目录
c>   在Test属性中配置Linker > Input > Additional Depencies 添加lualib.lib

7. main 函数调用

#include<iostream>
using namespace std;
//#pragma comment(lib,"lib//tlib.lib")  #include
<lua.hpp> int main() { lua_State *l = luaL_newstate(); luaL_openlibs(l); luaL_dofile(l, "main.lua"); lua_close(l); system("pause"); return 0; }

添加脚本:main.lua

print("hello world")

原创:http://www.cnblogs.com/cydonia/

=====================================================

8. lua 调用 cpp

lua_State* L;
static int average(lua_State *L)
{
    //返回栈中元素的个数
    int n = lua_gettop(L);
    double sum = 0;
    int i;
    for (i = 1; i <= n; i++)
    {
        if (!lua_isnumber(L, i)) 
        {
            lua_pushstring(L, "Incorrect argument to 'average'");
            lua_error(L);
        }
        sum += lua_tonumber(L, i);
    }
    /* push the average */
    lua_pushnumber(L, sum / n);
    /* push the sum */
    lua_pushnumber(L, sum);
    
    /* return the number of results */
    return 2;
}

int _tmain(int argc, _TCHAR* argv[])
{
    /* initialize Lua */
    L = luaL_newstate();
    /* load Lua libraries */
    luaL_openlibs(L);
    /* register our function */
    lua_register(L, "average", average);
    /* run the script */
    luaL_dofile(L, "lua_call_cpp.lua");
    
    lua_getglobal(L,"avg");
    cout<<"avg is:"<<lua_tointeger(L,-1)<<endl;
    lua_pop(L,1);
    lua_getglobal(L,"sum");
    cout<<"sum is:"<<lua_tointeger(L,-1)<<endl;
    /* cleanup Lua */
    lua_close(L);
    system("pause");

    return 0;
}
lua_call_cpp.lua 
avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)

输出:

The average is  30.0
The sum is      150.0
avg is:30
sum is:150

9. cpp 调用 lua

a>  如上上(main.lua) 直接执行lua 文件

b> 调用function

/* A simple Lua interpreter. */ 
#include <stdio.h> 
extern "C" {
#include <lua.h> 
#include <lualib.h>
#include <lauxlib.h>
}
#include <stdio.h>
extern "C" { // 这是个C++程序, 所以要extern "C",
    // 因为lua的头文件都是C格式的
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#pragma comment(lib, "lua5.1.lib")
/* the Lua interpreter */
lua_State* L;
int luaadd ( int x, int y )
{
    int sum;
    /* the function name */
    lua_getglobal(L, "add");        int nTop = lua_gettop(L); //得到栈的元素个数。栈顶的位置。
    /* the first argument */
    lua_pushnumber(L, x);            nTop = lua_gettop(L);
    /* the second argument */
    lua_pushnumber(L, y);            nTop = lua_gettop(L);
    /* call the function with 2
    arguments, return 1 result */
    lua_call(L, 2, 1);                nTop = lua_gettop(L);
    /* get the result */
    sum = (int)lua_tonumber(L, -1);    nTop = lua_gettop(L);
    /*清掉返回值*/
    lua_pop(L, 1);                    nTop = lua_gettop(L);
    /*取出脚本中的变量z的值*/
    lua_getglobal(L, "z");            nTop = lua_gettop(L);
    int z = (int)lua_tonumber(L, 1);nTop = lua_gettop(L);
    lua_pop(L, 1);                    nTop = lua_gettop(L);
    
    //没调通
    /*lua_pushnumber(L, 4);            nTop = lua_gettop(L);
    lua_setglobal(L, "r");            nTop = lua_gettop(L);
    int r = (int)lua_tonumber(L, 1);nTop = lua_gettop(L);*/
    return sum;
}
int main ( int argc, char *argv[] )
{
    int sum;
    /* initialize Lua */
    L = lua_open();
    /* load Lua base libraries */
    //lua_baselibopen(L);
    /* load the script */
    luaL_dofile(L, "e12.lua");
    /* call the add function */
    sum = luaadd( 10, 15 );
    /* print the result */
    printf( "The sum is %d", sum );
    /* cleanup Lua */
    lua_close(L);
    return 0;
}
/*程序说明:
main中过程偶们上次已经说过了, 所以这次只说说luaadd的过程
* 首先用lua_getglobal()把add函数压栈
* 然后用lua_pushnumber()依次把x,y压栈
* 然后调用lua_call(), 并且告诉程序偶们有两个参数一个返回值
* 接着偶们从栈顶取回返回值, 用lua_tonumber()
* 最后偶们用lua_pop()把返回值清掉
*/
e12.lua
-- add two numbers
function add ( x, y )
    return x + y + 2
end

转:http://blog.csdn.net/sndaxdrs/article/details/6230999

posted on 2015-05-18 15:24  AKever  阅读(1546)  评论(0)    收藏  举报