• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            lxyfirst

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              33 Posts :: 3 Stories :: 27 Comments :: 0 Trackbacks
            lua作為小巧精悍的腳本語言,易于嵌入c/c++中 , 廣泛應(yīng)用于游戲AI ,實(shí)際上在任何經(jīng)常變化的邏輯上都可以使用lua實(shí)現(xiàn),配合c/c++實(shí)現(xiàn)的底層接口服務(wù),能夠大大降低系統(tǒng)的維護(hù)成本。下面對(duì)lua和c/c++的交互調(diào)用做一個(gè)實(shí)例分析:
            lua提供了API用于在c/c++中構(gòu)造lua的運(yùn)行環(huán)境,相關(guān)接口如下:
            //創(chuàng)建lua運(yùn)行上下文
            lua_State* luaL_newstate(void) ;
            //加載lua腳本文件
            int luaL_loadfile(lua_State *L, const char *filename);

            lua和c/c++的數(shù)據(jù)交互通過"棧"進(jìn)行 ,操作數(shù)據(jù)時(shí),首先將數(shù)據(jù)拷貝到"棧"上,然后獲取數(shù)據(jù),棧中的每個(gè)數(shù)據(jù)通過索引值進(jìn)行定位,索引值為正時(shí)表示相對(duì)于棧底的偏移索引,索引值為負(fù)時(shí)表示相對(duì)于棧頂?shù)钠扑饕饕狄?或-1為起始值,因此棧頂索引值永遠(yuǎn)為-1 ,棧底索引值永遠(yuǎn)為1 。 "棧"相當(dāng)于數(shù)據(jù)在lua和c/c++之間的中轉(zhuǎn)地。每種數(shù)據(jù)都有相應(yīng)的存取接口 。
            數(shù)據(jù)入"棧"接口:
            void  (lua_pushnil) (lua_State *L);
            void  (lua_pushnumber) (lua_State *L, lua_Number n);
            void  (lua_pushinteger) (lua_State *L, lua_Integer n);
            void  (lua_pushlstring) (lua_State *L, const char *s, size_t l);
            void  (lua_pushstring) (lua_State *L, const char *s);
            void  (lua_pushboolean) (lua_State *L, int b);
            void  (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);

            數(shù)據(jù)獲取接口:
            lua_Number      (lua_tonumber) (lua_State *L, int idx);
            lua_Integer     (lua_tointeger) (lua_State *L, int idx);
            int             (lua_toboolean) (lua_State *L, int idx);
            const char     *(lua_tolstring) (lua_State *L, int idx, size_t *len);
            lua_CFunction   (lua_tocfunction) (lua_State *L, int idx);


            "棧"操作接口:
            int   (lua_gettop) (lua_State *L);
            void  (lua_settop) (lua_State *L, int idx);
            void  (lua_pushvalue) (lua_State *L, int idx);
            void  (lua_remove) (lua_State *L, int idx);
            void  (lua_insert) (lua_State *L, int idx);
            void  (lua_replace) (lua_State *L, int idx);
            int   (lua_checkstack) (lua_State *L, int sz);

            lua中定義的變量和函數(shù)存放在一個(gè)全局table中,索引值為LUA_GLOBALSINDEX ,table相關(guān)操作接口:
            void  (lua_gettable) (lua_State *L, int idx);
            void  (lua_getfield) (lua_State *L, int idx, const char *k);
            void  (lua_settable) (lua_State *L, int idx);
            void  (lua_setfield) (lua_State *L, int idx, const char *k);

            當(dāng)"棧"中包含執(zhí)行腳本需要的所有要素(函數(shù)名和參數(shù))后,調(diào)用lua_pcall執(zhí)行腳本:
            int   (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);

            下面進(jìn)行實(shí)例說明:
            func.lua
            --變量定義
            width
            =1 ;
            height
            =2 ;
            --lua函數(shù)定義,實(shí)現(xiàn)加法
            function sum(a,b)
                
            return a+b ;
            end
            --lua函數(shù)定義,實(shí)現(xiàn)字符串相加
            function mystrcat(a,b)
                
            return a..b ;
            end
            --lua函數(shù)定義,通過調(diào)用c代碼中的csum函數(shù)實(shí)現(xiàn)加法
            function mysum(a,b)
                
            return csum(a,b) ;
            end

            test_lua.c
            #include <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <string.h>
            #include <errno.h>
            //lua頭文件
            #include <lua.h>
            #include 
            <lualib.h>
            #include 
            <lauxlib.h>


            #define err_exit(num,fmt,args)  \
                
            do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);exit(num);} while(0)
            #define err_return(num,fmt,args)  \
                
            do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);return(num);} while(0)

            //lua中調(diào)用的c函數(shù)定義,實(shí)現(xiàn)加法
            int csum(lua_State* l)
            {
                
            int a = lua_tointeger(l,1) ;
                
            int b = lua_tointeger(l,2) ;
                lua_pushinteger(l,a
            +b) ;
                
            return 1 ;
            }

            int main(int argc,char** argv)
            {
                lua_State 
            * l = luaL_newstate() ;        //創(chuàng)建lua運(yùn)行環(huán)境
                
            if ( l == NULL ) err_return(-1,"luaL_newstat() failed"); 
                
            int ret = 0 ;
                ret 
            = luaL_loadfile(l,"func.lua") ;      //加載lua腳本文件
                
            if ( ret != 0 ) err_return(-1,"luaL_loadfile failed") ;
                ret 
            = lua_pcall(l,0,0,0) ;
                
            if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;

                lua_getglobal(l,
            "width");              //獲取lua中定義的變量
                lua_getglobal(l,
            "height");
                printf(
            "height:%ld width:%ld\n",lua_tointeger(l,-1),lua_tointeger(l,-2)) ;
                lua_pop(l,
            1) ;                        //恢復(fù)lua的棧

                
            int a = 11 ;
                
            int b = 12 ;
                lua_getglobal(l,
            "sum");               //調(diào)用lua中的函數(shù)sum
                lua_pushinteger(l,a) ;
                lua_pushinteger(l,b) ;
                ret 
            = lua_pcall(l,2,1,0) ;
                
            if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
                printf(
            "sum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
                lua_pop(l,
            1) ;

                
            const char str1[] = "hello" ;
                
            const char str2[] = "world" ;
                lua_getglobal(l,
            "mystrcat");          //調(diào)用lua中的函數(shù)mystrcat
                lua_pushstring(l,str1) ;
                lua_pushstring(l,str2) ;
                ret 
            = lua_pcall(l,2,1,0) ;
                
            if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
                printf(
            "mystrcat:%s%s = %s\n",str1,str2,lua_tostring(l,-1)) ;
                lua_pop(l,
            1) ;

                lua_pushcfunction(l,csum) ;         //注冊在lua中使用的c函數(shù)
                lua_setglobal(l,
            "csum") ;           //綁定到lua中的名字csum

                lua_getglobal(l,
            "mysum");           //調(diào)用lua中的mysum函數(shù),該函數(shù)調(diào)用本程序中定義的csum函數(shù)實(shí)現(xiàn)加法
                lua_pushinteger(l,a) ;
                lua_pushinteger(l,b) ;
                ret 
            = lua_pcall(l,2,1,0) ;
                
            if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
                printf(
            "mysum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
                lua_pop(l,
            1) ;

                lua_close(l) ;                     //釋放lua運(yùn)行環(huán)境
                
            return 0 ;
            }



            posted on 2008-10-29 14:37 star 閱讀(28576) 評(píng)論(12)  編輯 收藏 引用

            Feedback

            # re: lua和c/c++互相調(diào)用實(shí)例分析 2008-11-01 11:00 金山詞霸2008
            有個(gè)疑問,C++已經(jīng)足夠強(qiáng)大,為什么還需要lua腳本語言?  回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析 2008-11-03 18:45 diwayou
            靈活性!  回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析 2008-11-03 20:30 amuro1987218
            除去圖像這一層極端效率敏感的部分外,其他部分其實(shí)用效率適當(dāng)下降換取靈活是值得的。  回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析[未登錄] 2008-12-19 17:00 hunter
            這些都是簡單數(shù)據(jù)類型的操作,能不能提供一個(gè)操作struct(里面還有struct)的例子啊。  回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析 2009-01-06 20:34 lua
            寫的非常好,我折騰兩三天都沒有搞定.看了之后我全明白了.  回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析 2011-11-09 14:00 無賴二號(hào)
            非常好,非常感謝。  回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析[未登錄] 2012-04-24 14:56 jerome
            為什么我不能編譯

            ]# gcc testlua.C -llua -lm -ldl -I/usr/local/include/ -L/usr/local/lib/ -o testlua
            /tmp/ccIkmvNG.o: In function `csum(lua_State*)':
            testlua.C:(.text+0x16): undefined reference to `lua_tointeger(lua_State*, int)'
            testlua.C:(.text+0x27): undefined reference to `lua_tointeger(lua_State*, int)'
            testlua.C:(.text+0x3c): undefined reference to `lua_pushinteger(lua_State*, long)'
            /tmp/ccIkmvNG.o: In function `main':
            testlua.C:(.text+0x59): undefined reference to `luaL_newstate()'
            testlua.C:(.text+0x8d): undefined reference to `luaL_loadfile(lua_State*, char const*)'
            testlua.C:(.text+0xc4): undefined reference to `lua_pcall(lua_State*, int, int, int)'
            testlua.C:(.text+0xf6): undefined reference to `lua_getfield(lua_State*, int, char const*)'
            testlua.C:(.text+0x109): undefined reference to `lua_getfield(lua_State*, int, char const*)'
            testlua.C:(.text+0x117): undefined reference to `lua_tointeger(lua_State*, int)'
            testlua.C:(.text+0x128): undefined reference to `lua_tointeger(lua_State*, int)'
            testlua.C:(.text+0x14b): undefined reference to `lua_settop(lua_State*, int)'
            testlua.C:(.text+0x16c): undefined reference to `lua_getfield(lua_State*, int, char const*)'
            testlua.C:(.text+0x17b): undefined reference to `lua_pushinteger(lua_State*, long)'
            testlua.C:(.text+0x18a): undefined reference to `lua_pushinteger(lua_State*, long)'
            testlua.C:(.text+0x1a2): undefined reference to `lua_pcall(lua_State*, int, int, int)'
            testlua.C:(.text+0x1be): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
            testlua.C:(.text+0x1ea): undefined reference to `lua_tointeger(lua_State*, int)'
            testlua.C:(.text+0x210): undefined reference to `lua_settop(lua_State*, int)'
            testlua.C:(.text+0x24b): undefined reference to `lua_getfield(lua_State*, int, char const*)'
            testlua.C:(.text+0x258): undefined reference to `lua_pushstring(lua_State*, char const*)'
            testlua.C:(.text+0x265): undefined reference to `lua_pushstring(lua_State*, char const*)'
            testlua.C:(.text+0x27d): undefined reference to `lua_pcall(lua_State*, int, int, int)'
            testlua.C:(.text+0x299): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
            testlua.C:(.text+0x2ca): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
            testlua.C:(.text+0x2f2): undefined reference to `lua_settop(lua_State*, int)'
            testlua.C:(.text+0x305): undefined reference to `lua_pushcclosure(lua_State*, int (*)(lua_State*), int)'
            testlua.C:(.text+0x318): undefined reference to `lua_setfield(lua_State*, int, char const*)'
            testlua.C:(.text+0x32b): undefined reference to `lua_getfield(lua_State*, int, char const*)'
            testlua.C:(.text+0x33a): undefined reference to `lua_pushinteger(lua_State*, long)'
            testlua.C:(.text+0x349): undefined reference to `lua_pushinteger(lua_State*, long)'
            testlua.C:(.text+0x361): undefined reference to `lua_pcall(lua_State*, int, int, int)'
            testlua.C:(.text+0x37d): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
            testlua.C:(.text+0x3a6): undefined reference to `lua_tointeger(lua_State*, int)'
            testlua.C:(.text+0x3cc): undefined reference to `lua_settop(lua_State*, int)'
            testlua.C:(.text+0x3d5): undefined reference to `lua_close(lua_State*)'
            /tmp/ccIkmvNG.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
            collect2: ld returned 1 exit status
              回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析[未登錄] 2012-04-26 17:43 tommy
            請(qǐng)看清楚了作者寫的文件名是test_lua.c
            如果你保存為cpp文件了,關(guān)于lua相關(guān)頭文件應(yīng)該使用:
            extern "C"{
            #include "lua.h"
            #include "lualib.h"
            #include "lauxlib.h"
            }
            否則編譯會(huì)提示你的那種問題  回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析 2012-05-03 10:20 star
            @jerome
            鏈接時(shí)找不到函數(shù)定義,這個(gè)應(yīng)該是lua庫的路徑問題。  回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析 2014-02-02 14:46 ixil
            @hunter
            point=alien.buffer
            賦值
            x=point:get(1,"long")
            y=point:get(5,"long")  回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析 2014-03-13 18:50 fy
            lua_pop(l,1) ; //恢復(fù)lua的棧

            這一行應(yīng)該改為 lua_pop(l,2) ;   回復(fù)  更多評(píng)論
              

            # re: lua和c/c++互相調(diào)用實(shí)例分析 2014-11-29 13:54 iHuahua
            @金山詞霸2008
            術(shù)業(yè)有專攻,C++那么強(qiáng)大,你倒是用他寫個(gè)網(wǎng)頁我看看  回復(fù)  更多評(píng)論
              


            只有注冊用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            一本大道加勒比久久综合| 69SEX久久精品国产麻豆| 久久e热在这里只有国产中文精品99 | 国内精品久久久久影院免费| 久久综合色老色| 久久99久国产麻精品66| 天堂无码久久综合东京热| 99精品久久久久久久婷婷| 伊人久久大香线蕉综合Av| 久久w5ww成w人免费| 久久九九亚洲精品| 久久狠狠色狠狠色综合| 欧美粉嫩小泬久久久久久久 | 国内精品久久久久久久久电影网| 2022年国产精品久久久久| 99久久国产综合精品麻豆| 亚洲?V乱码久久精品蜜桃| 18岁日韩内射颜射午夜久久成人| 99久久精品免费看国产一区二区三区 | 99精品久久久久久久婷婷| www久久久天天com| 久久99精品国产99久久6男男| 国产精品成人久久久久三级午夜电影| 欧洲人妻丰满av无码久久不卡| 久久综合九色综合网站| 亚洲国产高清精品线久久| 久久综合色老色| 久久久久久久亚洲Av无码| 青青青国产成人久久111网站| 精品久久久久久久久久久久久久久| 97超级碰碰碰碰久久久久| 精品国产91久久久久久久a| 精品国产日韩久久亚洲| 狠狠色丁香久久婷婷综| 午夜精品久久久久久影视riav| 久久丫精品国产亚洲av不卡 | 麻豆av久久av盛宴av| 99久久精品费精品国产一区二区| 久久久精品久久久久久| 久久青青草原精品国产| 三级片免费观看久久|