青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

lxyfirst

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

lua和c/c++的數據交互通過"棧"進行 ,操作數據時,首先將數據拷貝到"棧"上,然后獲取數據,棧中的每個數據通過索引值進行定位,索引值為正時表示相對于棧底的偏移索引,索引值為負時表示相對于棧頂的偏移索引,索引值以1或-1為起始值,因此棧頂索引值永遠為-1 ,棧底索引值永遠為1 。 "棧"相當于數據在lua和c/c++之間的中轉地。每種數據都有相應的存取接口 。
數據入"棧"接口:
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);

數據獲取接口:
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中定義的變量和函數存放在一個全局table中,索引值為LUA_GLOBALSINDEX ,table相關操作接口:
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);

當"棧"中包含執行腳本需要的所有要素(函數名和參數)后,調用lua_pcall執行腳本:
int   (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);

下面進行實例說明:
func.lua
--變量定義
width
=1 ;
height
=2 ;
--lua函數定義,實現加法
function sum(a,b)
    
return a+b ;
end
--lua函數定義,實現字符串相加
function mystrcat(a,b)
    
return a..b ;
end
--lua函數定義,通過調用c代碼中的csum函數實現加法
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中調用的c函數定義,實現加法
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() ;        //創建lua運行環境
    
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) ;                        //恢復lua的棧

    
int a = 11 ;
    
int b = 12 ;
    lua_getglobal(l,
"sum");               //調用lua中的函數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");          //調用lua中的函數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函數
    lua_setglobal(l,
"csum") ;           //綁定到lua中的名字csum

    lua_getglobal(l,
"mysum");           //調用lua中的mysum函數,該函數調用本程序中定義的csum函數實現加法
    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運行環境
    
return 0 ;
}



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

Feedback

# re: lua和c/c++互相調用實例分析 2008-11-01 11:00 金山詞霸2008
有個疑問,C++已經足夠強大,為什么還需要lua腳本語言?  回復  更多評論
  

# re: lua和c/c++互相調用實例分析 2008-11-03 18:45 diwayou
靈活性!  回復  更多評論
  

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

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

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

# re: lua和c/c++互相調用實例分析 2011-11-09 14:00 無賴二號
非常好,非常感謝。  回復  更多評論
  

# re: lua和c/c++互相調用實例分析[未登錄] 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
  回復  更多評論
  

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

# re: lua和c/c++互相調用實例分析 2012-05-03 10:20 star
@jerome
鏈接時找不到函數定義,這個應該是lua庫的路徑問題。  回復  更多評論
  

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

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

這一行應該改為 lua_pop(l,2) ;   回復  更多評論
  

# re: lua和c/c++互相調用實例分析 2014-11-29 13:54 iHuahua
@金山詞霸2008
術業有專攻,C++那么強大,你倒是用他寫個網頁我看看  回復  更多評論
  

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            午夜视频在线观看一区| 亚洲视频免费| 在线中文字幕一区| 欧美激情一区二区三区不卡| 午夜精品一区二区三区在线播放 | 欧美激情久久久久| 久久人人97超碰国产公开结果| 亚洲国产aⅴ天堂久久| 免费一级欧美片在线播放| 久久久久久久999精品视频| 国产视频久久网| 久久久噜噜噜久久| 亚洲精品视频在线播放| 亚洲欧美日韩国产综合| 亚洲伦伦在线| 久久久久久网| 欧美一区午夜精品| 欧美精品日韩一本| 欧美人妖在线观看| 欧美一级理论片| 国产精品嫩草99a| 亚洲国产乱码最新视频| 一色屋精品视频免费看| 亚洲图片欧洲图片av| 99这里只有精品| 欧美精品99| 亚洲精品美女在线观看| 精品成人久久| 久久久水蜜桃av免费网站| 亚洲香蕉网站| 99国内精品久久| 欧美日韩亚洲一区二区| 91久久嫩草影院一区二区| 亚洲日韩中文字幕在线播放| 欧美在线看片a免费观看| 欧美在线影院| 国产欧美综合在线| 亚洲影视综合| 久久成人资源| 国产欧美亚洲一区| 久久一区二区三区av| 欧美国产日韩xxxxx| 亚洲国产精品第一区二区三区| 久久深夜福利免费观看| 欧美国产精品v| 一区二区三区高清不卡| 国产日韩专区| 欧美激情一区在线| 亚洲永久字幕| 欧美成人国产| 久久精品成人一区二区三区蜜臀| 精品粉嫩aⅴ一区二区三区四区| 欧美电影在线观看完整版| 99人久久精品视频最新地址| 欧美 日韩 国产在线| 性欧美video另类hd性玩具| 美女视频黄 久久| 亚洲在线第一页| 亚洲午夜高清视频| 亚洲婷婷在线| 欧美在线视频导航| 亚洲国产高潮在线观看| 欧美中文字幕视频在线观看| 亚洲精选一区| 亚洲高清在线视频| 狠狠操狠狠色综合网| 国产老女人精品毛片久久| 欧美午夜一区二区三区免费大片| 欧美激情视频在线播放| 另类国产ts人妖高潮视频| 久久www免费人成看片高清| 欧美一区视频在线| 久久不射中文字幕| 久久久人成影片一区二区三区| 久久午夜精品一区二区| 欧美日韩激情网| 欧美日韩一区二区三区免费| 欧美日韩国产不卡在线看| 欧美日韩精品在线视频| 国产精品热久久久久夜色精品三区 | 欧美资源在线| 欧美专区亚洲专区| 欧美国产日韩一区| 91久久国产综合久久蜜月精品| 91久久综合亚洲鲁鲁五月天| 亚洲激情视频| 亚洲一区二区动漫| 久久一区亚洲| 国产精品捆绑调教| 亚洲电影视频在线| 亚洲尤物影院| 亚洲电影在线| 亚洲欧美在线磁力| 欧美高清视频免费观看| 91久久综合亚洲鲁鲁五月天| 一区二区三区精密机械公司| 久久国产天堂福利天堂| 欧美日韩极品在线观看一区| 国内精品嫩模av私拍在线观看| av72成人在线| 欧美大香线蕉线伊人久久国产精品| 一区二区国产在线观看| 欧美一区观看| 国产亚洲一区在线| 亚洲永久精品大片| 亚洲精品老司机| 欧美精品v国产精品v日韩精品| 伊人狠狠色j香婷婷综合| 久久国产主播| 欧美一区二区在线观看| 国产欧美精品久久| 久久精品国产99国产精品澳门| 99精品国产一区二区青青牛奶| 欧美—级高清免费播放| 亚洲电影天堂av| 亚洲激情成人网| 欧美日韩直播| 亚洲欧美日韩直播| 欧美一区二视频| 亚洲福利视频二区| 亚洲国产一区二区三区青草影视| 欧美精品www| 亚洲综合色激情五月| 亚洲一区日韩在线| 精品va天堂亚洲国产| 欧美激情视频给我| 欧美午夜不卡视频| 久久人人爽人人爽爽久久| 欧美xxxx在线观看| 亚洲欧美精品在线| 久久综合九色综合欧美狠狠| 99国产精品久久久久久久| 亚洲一区二区精品在线| 亚洲电影一级黄| 亚洲私人影院在线观看| 亚洲国产精品高清久久久| 夜夜嗨一区二区三区| 亚洲精品一区二区三区不| 亚洲视频香蕉人妖| 欧美影院在线播放| 一本色道久久综合| 六月婷婷一区| 久久久久.com| 国产欧美一区二区色老头| 亚洲国产日韩欧美在线99| 韩国在线一区| 久久精品视频99| 久久久国产精品一区| 国产精品美女午夜av| 日韩午夜三级在线| 99riav久久精品riav| 欧美freesex交免费视频| 噜噜噜91成人网| 狠狠色狠狠色综合| 久久国产精彩视频| 久久综合久久88| 136国产福利精品导航网址应用| 欧美专区在线播放| 久久久久久电影| 亚洲国产精品ⅴa在线观看 | 欧美激情亚洲国产| 亚洲激情婷婷| 亚洲无线视频| 国产精品免费观看视频| 一区二区三区av| 久久久91精品国产一区二区三区| 国产精品午夜久久| 另类综合日韩欧美亚洲| 亚洲免费电影在线观看| 久久久欧美一区二区| 亚洲国产成人不卡| 国产精品mm| 久久久噜噜噜久久中文字幕色伊伊| 欧美国产欧美综合| 亚洲伊人久久综合| 激情综合网激情| 欧美性事在线| 免费亚洲电影在线| 欧美在线91| 在线视频你懂得一区| 欧美国产视频一区二区| 午夜精彩视频在线观看不卡| 亚洲精品少妇30p| 亚洲国产成人av好男人在线观看| 国产精品自拍三区| 国产精品高潮呻吟久久| 欧美国产一区二区| 亚洲第一精品福利| 久久夜色撩人精品| 亚洲图片在线| 国产精品99久久久久久久女警| 在线日韩一区二区| 悠悠资源网久久精品| 亚洲电影中文字幕| 亚洲福利视频一区| 亚洲九九精品| 一本在线高清不卡dvd| 一区二区激情| 久久国产精品久久久久久电车| 欧美一区二区三区免费看|