• <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>

            luabind和c++相互調用


            先上代碼:

            #include 
            "stdafx.h"
            #include
            <iostream>
            #include 
            "luabind\luabind.hpp"

            extern "C"
            {
            #include 
            "lua.h"
            #include 
            "lualib.h"
            #include 
            "lauxlib.h"
            }


            using namespace std;

            bool LoadScript(lua_State *L,const string& fname)
            {
                
            if (luaL_dofile(L,fname.c_str()))
                
            {
                    cerr
            <<lua_tostring(L,-1)<<endl;
                    
            return false;
                }
             
                
            return true;
            }


            void testFunc(int k)
            {
                cout
            <<"hello there, i am a cpp fun"<<endl;
                cout
            <<"input num:="<<k<<endl;
            }


            class NumberPrinter {
            public:
                NumberPrinter(
            int number) :
                  m_number(number) 
            {}

                  
            void print() {
                      cout 
            << m_number << endl;
                  }


            private:
                
            int m_number;
            }
            ;


            int _tmain(int argc, _TCHAR* argv[])
            {
                
            using namespace luabind;
                lua_State
            * L = luaL_newstate();
                module(L, 
            "cppapi")
                    [
                        def(
            "testFunc", (void(*)(int))testFunc)
                    ];

                
            // 使用LuaBind導出NumberPrinter類
                luaopen_base(L);
                luabind::open(L);

                luabind::module(L) 
                    [
                        luabind::class_
            <NumberPrinter>("NumberPrinter")
                        .def(luabind::constructor
            <int>())
                        .def(
            "print"&NumberPrinter::print)
                    ];
                
                luaL_openlibs(L);
                LoadScript(L,
            "test.lua");
                
            try{
                     
            int add_ret = luabind::call_function<int>(L,"add",10,4);
                     
            int call_global = luabind::object_cast<int>(luabind::globals(L)["nGlobal"]);
                     
            string strGlobal = luabind::object_cast<string>(luabind::globals(L)["strGlobal"]) ;
                     luabind::
            object lua_object = luabind::globals(L)["t"];
                     
            //--2種辦法load table
                     string strName = luabind::object_cast<string>(lua_object["name"]);
                     
            int iAge = luabind::object_cast<int>(lua_object["age"]);
                     
            string strElse = luabind::object_cast<string>(lua_object["desc"]);
                }

                
            catch(luabind::error& e)
                
            {
                    cout
            <<e.what()<<endl;
                    printf(
            "AI throw error: err_msg[%s]", lua_tostring(L, -1));
                    
            return false;
                }

                
                lua_close(L);
                
            return 0;
            }

            其中test.lua的代碼如下:
            Print2000 = NumberPrinter(2000)
            Print2000:print()

            nGlobal = 10 --一個全局的整形變量
            strGlobal = "hello i am in lua" --一個全局的字符串變量
            --一個返回值為int類型的函數
            function add(a, b)
                return a+b
            end
            --一個返回值為string類型的函數
            function strEcho(a)
                print(a)
                return 'haha i h

            ave print your input param'
            end

            cppapi.testFunc(10) --調用c++暴露的一個測試函數
            t={name='ettan', age=23, desc='正值花季年齡'}

            運行結果為:



            這上面luabind調用c++函數的實例:調用testFunc函數;也有c++調用lua的代碼,具體的見代碼。
            此代碼在我的vs2010上面調試通過,前提是必須配好環境如:添加依賴庫luabind.debug.lib;lua.debug.lib
            添加依賴庫路徑。

            特別注意:

            首先Lua是“動態編譯的腳本語言”,而loadfile只是把源文件加載到內存中,還少了“編譯”這一步,可以用“luaL_dofile(L,"test.lua");”來替換,它既加載又編譯。替換之后執行應該就沒有問題了。

            但是還沒完,luaL_dofile 實際上是個宏:

             

            1. #define luaL_dofile(L, fn) \ 
            2. (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))

             

            LUA_MULTRET也是宏定義,值為-1,表示函數有多個返回值(Lua規則,pil 24.2--堆棧)。

            擴展開來就是以下兩句:

            1. luaL_loadfile(L, fn);
            2. lua_pcall(L, 0, LUA_MULTRET, 0);

            pcall以上述參數執行的時候,會把加載到內存中的源程序編譯成可以用于執行的2進制代碼,并將全局變量壓棧(在Lua中,函數也是變量,pil 2.5 -- Functions,畢竟函數名和函數體是不同的2個東西)。就跟PE文件格式里的Section一樣(PE文件就是Windows3.1之后的.exe/.dll文件)。當然如果你不知道什么PE文件也沒關系--我只是打個比方--就當成VS2005編譯代碼時生成的.obj文件。

             

             

            雖然實際使用中99%的情況都是直接使用dofile,但是我想將該問題提出來說可以更加直觀的理解“動態編譯”。

            參照:
            代碼和文字大部分出自 1.   http://blog.csdn.net/caoyanting007/article/details/5709820
                                         2.   http://mobile.51cto.com/iphone-285654.htm

            posted on 2012-11-13 10:46 sheng 閱讀(3679) 評論(0)  編輯 收藏 引用

            導航

            <2012年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            收藏夾

            同行

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久天天躁夜夜躁狠狠| 久久人人爽人人爽人人片av麻烦| 天堂久久天堂AV色综合| 久久天堂AV综合合色蜜桃网| 成人免费网站久久久| 久久久久亚洲精品男人的天堂| 色偷偷88欧美精品久久久 | 99久久综合狠狠综合久久| 久久www免费人成看国产片| 久久受www免费人成_看片中文| 日产精品99久久久久久| 色综合久久久久网| 亚洲精品tv久久久久久久久| 美女写真久久影院| 一本色道久久88精品综合| 国产精品久久久久9999| 久久人妻AV中文字幕| 精品国产青草久久久久福利| 亚洲精品美女久久久久99| 久久婷婷五月综合97色直播| AV狠狠色丁香婷婷综合久久| 一级A毛片免费观看久久精品| 伊人久久免费视频| 77777亚洲午夜久久多喷| 久久久久精品国产亚洲AV无码| 精品国产热久久久福利| 久久美女人爽女人爽| 久久精品国产久精国产思思| 亚洲国产视频久久| 久久中文精品无码中文字幕| aaa级精品久久久国产片| 三上悠亚久久精品| 欧美亚洲色综久久精品国产| 亚洲欧美另类日本久久国产真实乱对白| 91精品国产乱码久久久久久| 久久久一本精品99久久精品88| 午夜精品久久久内射近拍高清| 欧美粉嫩小泬久久久久久久 | 国产—久久香蕉国产线看观看| 久久99免费视频| 久久亚洲国产精品一区二区|