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

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 閱讀(28646) 評論(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++那么強大,你倒是用他寫個網頁我看看  回復  更多評論
  


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲第一区色| 亚洲国产婷婷| 久久精视频免费在线久久完整在线看 | 欧美日韩精品欧美日韩精品一 | 国产亚洲电影| 国产精品有限公司| 国产精品最新自拍| 一区二区在线视频| 亚洲裸体在线观看| 亚洲天堂男人| 久久免费视频网站| 亚洲国产精品一区| 亚洲肉体裸体xxxx137| 中文av字幕一区| 小处雏高清一区二区三区| 久久亚洲一区| 欧美理论在线播放| 国产一区91精品张津瑜| 亚洲国产欧洲综合997久久| 一区二区三区免费看| 欧美在线一二三四区| 欧美国产亚洲精品久久久8v| 一区二区欧美在线| 久久夜色精品| 国产精品美女久久久久久2018 | 免费亚洲电影在线| 欧美日韩在线视频一区二区| 国产一区二区成人| 一区二区av| 麻豆久久久9性大片| 日韩小视频在线观看专区| 久久av最新网址| 国产精品黄页免费高清在线观看| 亚洲春色另类小说| 久久精品国产免费| 日韩视频在线观看| 欧美成人黑人xx视频免费观看| 国产精品夜夜夜| 亚洲天堂成人在线观看| 欧美国产视频一区二区| 久久黄色小说| 国产午夜亚洲精品理论片色戒| 亚洲无线观看| 999在线观看精品免费不卡网站| 蜜臀99久久精品久久久久久软件| 国产欧美精品一区| 亚洲欧美制服另类日韩| 99国产精品国产精品毛片| 亚洲日本在线观看| 欧美风情在线| 亚洲国产欧美不卡在线观看| 久久青青草综合| 欧美一区2区视频在线观看 | 一区二区三区日韩在线观看| 男同欧美伦乱| 亚洲国产精品999| 久久天天躁夜夜躁狠狠躁2022| 亚洲自拍电影| 国产日韩一区二区三区| 欧美在线中文字幕| 性久久久久久久久| 国模大胆一区二区三区| 久久久午夜精品| 久久国产欧美日韩精品| 狠狠色伊人亚洲综合成人| 久久久av毛片精品| 欧美在线资源| 亚洲国产精品福利| 蜜臀91精品一区二区三区| 久久精品72免费观看| 黄色小说综合网站| 欧美成人在线免费观看| 99热免费精品在线观看| 国产免费一区二区三区香蕉精| 亚洲综合激情| 欧美中文在线观看| 亚洲国产欧美一区二区三区同亚洲 | 亚洲精品久久久一区二区三区| 免播放器亚洲一区| 麻豆av福利av久久av| 99精品视频免费观看| 一区二区日韩| 国产一区二区三区成人欧美日韩在线观看 | 国产精品黄视频| 久久久久国产成人精品亚洲午夜| 久久精品导航| 一级日韩一区在线观看| 亚洲欧美激情诱惑| 最新国产拍偷乱拍精品 | 久热精品视频在线| 美女视频网站黄色亚洲| 99这里有精品| 亚洲欧洲一区二区三区在线观看| 欧美日韩亚洲一区在线观看| 久久不见久久见免费视频1| 噜噜噜在线观看免费视频日韩| 99精品国产在热久久下载| 午夜精品一区二区三区在线| 亚洲黄色尤物视频| 亚洲一区二区三区免费观看| 亚洲高清123| 亚洲一区二区精品视频| 在线不卡亚洲| 亚洲影视中文字幕| 亚洲精品一区二区三区av| 午夜精品免费视频| 一本久道综合久久精品| 久久蜜桃香蕉精品一区二区三区| 亚洲丝袜av一区| 欧美阿v一级看视频| 久久久久国产成人精品亚洲午夜| 欧美日韩日本视频| 欧美激情麻豆| 狠狠色丁香婷婷综合| 亚洲一区欧美一区| 一本大道久久a久久精品综合| 久久婷婷丁香| 久久久天天操| 国产亚洲精品一区二区| 亚洲免费精彩视频| 欧美黄免费看| 亚洲在线播放| 亚洲一区二区三区四区中文| 免费一区视频| 欧美成人国产va精品日本一级| 国产日韩1区| 亚洲欧美日韩中文播放| 亚洲综合成人婷婷小说| 欧美日韩一区在线观看| 亚洲福利在线视频| 在线免费观看日本一区| 久久精品国产欧美激情| 久久久精品一品道一区| 国产一区二区精品久久91| 亚洲主播在线观看| 香蕉视频成人在线观看| 女生裸体视频一区二区三区| 亚洲一区制服诱惑| 欧美日韩亚洲另类| 国产精品劲爆视频| 午夜在线精品偷拍| 亚洲午夜三级在线| 亚洲精品日本| 国产精品毛片va一区二区三区| 欧美高清在线精品一区| 伊人成人在线| 久久人人爽爽爽人久久久| 蜜臀av性久久久久蜜臀aⅴ四虎| 久久久青草青青国产亚洲免观| 久久蜜桃精品| 伊人精品视频| 蜜臀久久99精品久久久久久9| 欧美大香线蕉线伊人久久国产精品| 亚洲高清在线观看| 欧美日韩国产精品一卡| 亚洲综合第一| 久久亚洲精品中文字幕冲田杏梨| 在线国产日韩| 欧美区高清在线| 亚洲一区二区三| 免费不卡在线观看| 一本大道久久a久久综合婷婷 | 久久精品在线播放| 亚洲第一页自拍| 亚洲午夜视频| 国产日韩av一区二区| 久久久久久免费| 日韩视频免费在线| 久久久精品动漫| 日韩一区二区精品在线观看| 国产精品欧美一区喷水 | 国产视频一区在线观看| 欧美国产视频在线观看| 国产精品99久久久久久久vr| 久久精品综合| 亚洲国产精品一区| 国产模特精品视频久久久久 | 日韩亚洲在线| 欧美顶级大胆免费视频| 中文精品视频| 亚洲女人天堂成人av在线| 欧美99在线视频观看| 亚洲日本va午夜在线电影| 性欧美暴力猛交另类hd| 麻豆成人在线观看| 久久精品网址| 亚洲午夜精品视频| 亚洲一区视频| 国产亚洲精品成人av久久ww| 1024亚洲| 亚洲欧美国产日韩天堂区| 亚洲一级一区| 亚洲欧洲av一区二区| 亚洲黄色在线观看| 亚洲美女精品成人在线视频| 欧美日韩国产黄| 国产日产欧美精品| 午夜伦欧美伦电影理论片| 亚洲欧美一区二区原创| 久久亚洲一区二区|