• <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++相互調(diào)用


            先上代碼:

            #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導(dǎo)出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類型的函數(shù)
            function add(a, b)
                return a+b
            end
            --一個返回值為string類型的函數(shù)
            function strEcho(a)
                print(a)
                return 'haha i h

            ave print your input param'
            end

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

            運行結(jié)果為:



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

            特別注意:

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

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

             

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

             

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

            擴(kuò)展開來就是以下兩句:

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

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

             

             

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

            參照:
            代碼和文字大部分出自 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)  編輯 收藏 引用


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


            導(dǎo)航

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆檔案

            收藏夾

            同行

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            色综合久久中文综合网| 奇米综合四色77777久久| 精品久久久久久无码人妻蜜桃| 久久噜噜久久久精品66| 中文字幕久久精品 | 亚洲精品无码久久千人斩| 97久久超碰国产精品旧版| 久久久久亚洲?V成人无码| 亚洲国产欧洲综合997久久| 国产免费久久精品99久久| 久久久老熟女一区二区三区| 欧美激情精品久久久久久久| 久久婷婷成人综合色综合| 青春久久| 精品久久久久一区二区三区 | 久久久久一区二区三区| 伊人久久大香线蕉成人| 精品人妻伦九区久久AAA片69| 人妻无码αv中文字幕久久| 亚洲国产精品成人久久蜜臀 | 狠狠人妻久久久久久综合| 91精品国产9l久久久久| 丁香色欲久久久久久综合网| 亚洲国产成人精品女人久久久 | 久久综合给合久久国产免费| 99久久国产亚洲综合精品| 久久男人中文字幕资源站| 国产精品久久久久久久午夜片| 99久久超碰中文字幕伊人| 亚洲人成网亚洲欧洲无码久久 | 日本一区精品久久久久影院| 国产精品99久久99久久久| 久久精品无码专区免费青青| 亚洲av日韩精品久久久久久a| 久久国产色av免费看| 亚洲精品乱码久久久久久蜜桃图片 | 精品午夜久久福利大片| 72种姿势欧美久久久久大黄蕉| 久久九九精品99国产精品| 国产婷婷成人久久Av免费高清| 精品综合久久久久久97超人|