lua自己使用笔记

来源:互联网 发布:初学java看什么书 编辑:程序博客网 时间:2024/06/02 07:31
今天无聊,心血来潮想学学lua。学习步骤

1、按照lua开发环境(windows版) 从http://code.google.com/p/luaforwindows/downloads/list 下载LuaForWindows_v5.1.4-45.exe,和安装。点点点点。。。。 完成。

2、用vs2005新建个c++工程,把安装目录中的Lua\5.1\include 和 Lua\5.1\lib 两个文件拷贝到自己工程目录下,然后

     工程属性--->c/c++---->常规 --->附件包含目录 中加入 ../include 

    工程属性--->连接器----》常规 --->附加库目录- 中加入 ../lib

    工程属性--->连接器 --->输入-----》 附加依赖项 中加入 lua5.1.lib lua51.lib

反正有用没用都加上。 需要将两个dll文件也放到工程目录下。

新建个.cpp文件。从网上考个例子代码

#include <stdio.h> extern "C" { #include "lua.h" #include "lauxlib.h" #include "lualib.h" }int main(int argc, char *argv[]) {        lua_State *L = lua_open();//装载lua堆栈    luaL_openlibs(L);    if (NULL == L)    {        printf("Error Initializing lua\n");        return -1;    }    char line[BUFSIZ];    while (fgets(line, sizeof(line), stdin) != 0){             luaL_dostring(L,line);    }     lua_close(L);     return 0; }

编译,运行 输入

for i=1,5 do print(i) end

第一个事例就算完成

 

第二个事例 有网上找的代码

 

/* A simple Lua interpreter. */ #include <stdio.h> extern "C" { #include "lua.h" #include "lauxlib.h" #include "lualib.h" }/*int main(int argc, char *argv[]) {        lua_State *L = lua_open();//装载lua堆栈    luaL_openlibs(L);    if (NULL == L)    {        printf("Error Initializing lua\n");        return -1;    }    char line[BUFSIZ];    while (fgets(line, sizeof(line), stdin) != 0){             luaL_dostring(L,line);    }     lua_close(L);     return 0; }*/lua_State  * L;static void RestackDump(lua_State *L){    int i;    int top = lua_gettop(L);    for (i = -top; i <= -1; i++)    {        int t = lua_type(L, i);        switch(t)        {        case LUA_TSTRING:            printf("`%s`\n", lua_tostring(L, i));            break;        case LUA_TBOOLEAN:            printf("%d\n", lua_toboolean(L, i));            break;        case LUA_TNUMBER:            printf("%g\n", lua_tonumber(L, i));            break;        default:            printf("%s\n", lua_typename(L, t));            break;        }    }}static void stackDump(lua_State *L){    int i;    int top = lua_gettop(L);    for (i = 1; i <= top; i++)    {        int t = lua_type(L, i);        switch(t)        {        case LUA_TSTRING:            printf("`%s`\n", lua_tostring(L, i));            break;        case LUA_TBOOLEAN:            printf("%d\n", lua_toboolean(L, i));            break;        case LUA_TNUMBER:            printf("%g\n", lua_tonumber(L, i));            break;        default:            printf("%s\n", lua_typename(L, t));            break;        }    }}int luaadd ( int x, int y ){    int sum = 1;    //函数名    lua_getglobal(L, "min");    //第一个参数压栈    lua_pushnumber(L, x);    //第二个参数压栈    lua_pushnumber(L, y);    //调用函数    lua_call(L, 2, 1);   // stackDump(L);    //得到返回值    sum = (int)lua_tonumber(L, -1);    lua_pop(L, 1);    return sum;}int luafact(int x){    lua_getglobal(L, "fact");    lua_pushnumber(L, x);    lua_call(L, 1, 1);    int nResult = (int)lua_tonumber(L, -1);    lua_pop(L, 1);    return nResult;}char* luaTest(){    lua_getglobal(L, "testfun");    lua_call(L, 0, 1);    char *szString;    int nStackNum = lua_gettop(L);    stackDump(L);    szString = (char *)lua_tostring(L, -1);   // int nRet = (int)lua_tonumber(L, -2);    lua_pop(L, 1);    return szString;}int main ( int argc, char *argv[] ){    int nResult;    //创建一个指向Lua解释器的指针。    L = lua_open();    //函数加载Lua库    luaL_openlibs(L);            //加载脚本    if (luaL_dofile(L, "new.lua")) {        printf("%s\n", lua_tostring(L,-1)); // print the error message    }    char * pszRstStr = luaTest();   // int nValue = (int)lua_tonumber(L, 1);    //调用函数    nResult = luaadd( 13, 11);    // print the result    printf( "The sum is %d\n", nResult);    nResult = luafact(10);    printf("the fact is %d\n", nResult);    lua_pushnumber(L, 12);    lua_pushnumber(L, 15);    lua_pushnumber(L, 20);    lua_pushstring(L, "hello");    lua_pushboolean(L, 1);    RestackDump(L);    stackDump(L);    //关闭 释放资源       lua_close(L);    //   system("pause");    return 0;}


反正乱七八糟一起了,有的demo电子书上看到的。这里做个测试