加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua入门

发布时间:2020-12-14 22:21:00 所属栏目:大数据 来源:网络整理
导读:?? 最近两天没啥事,在研究一个开源游戏,发现其中用了Lua脚本语言,这个东西从来没接触过,所以在网上找了些个入门的小例子学习,但是过程中出现了许多的错误。 首先在网上读了一篇入门教程,有个例子可是却编译不过。 开发环境:OS:CentOS5.3 32位 ??????

?? 最近两天没啥事,在研究一个开源游戏,发现其中用了Lua脚本语言,这个东西从来没接触过,所以在网上找了些个入门的小例子学习,但是过程中出现了许多的错误。

首先在网上读了一篇入门教程,有个例子可是却编译不过。

开发环境:OS:CentOS5.3 32位

????????? Lua 5.2

代码如下:

文件 e12.lua

 
 
  1. --?add?two?numbers?
  2. function?add?(?x,?y?)?
  3. ??return?x?+?y?
  4. end?

?

文件 e13.cpp

 
 
  1. #include?<stdio.h>?
  2. extern?"C"?
  3. {?
  4. ????#include?"lua.h"?  ?
  5. ????#include?"lualib.h"?  ?
  6. ????#include?"lauxlib.h"?  ?
  7. }?  ?
  8. ?
  9. lua_State*?L;?  ?
  10. int?luaadd?(?int?x,?int?y?)?  ?
  11. {?  ?
  12. ????int?sum;?  ?
  13. ????lua_getglobal(L,?"add");?  ?
  14. ????lua_pushnumber(L,?x);?  ?
  15. ????lua_pushnumber(L,?y);?  ?
  16. ????lua_call(L,?2,?1);?  ?
  17. ????sum?=?(int)lua_tonumber(L,?-1);?  ?
  18. ????lua_pop(L,?1);?  ?
  19. ????
  20. ????return?sum;?  ?
  21. }?  ?
  22. int?main?(?int?argc,?char?*argv[]?)?  ?
  23. {?  ?
  24. ????int?sum;?  ?
  25. ????L?=?lua_open();?  ?
  26. ????lua_baselibopen(L);?  ?
  27. ????lua_dofile(L,?"e12.lua");?  ?
  28. ????sum?=?luaadd(?10,?15?);?  ?
  29. ????printf(?"The?sum?is?%dn",?sum?);?  ?
  30. ?
  31. ????lua_close(L);?  ?
  32. ????
  33. ????return?0;?  ?
  34. }?

编译方法:

g++ e13.cpp -llua -llualib -o e13

然后发现出现错误:

e13.cpp: In function ‘int main(int,char**)’:
e13.cpp:31: error: ‘lua_open’ was not declared in this scope
e13.cpp:34: error: ‘lua_baselibopen’ was not declared in this scope
e13.cpp:38: error: ‘lua_dofile’ was not declared in this scope

在参看了网上其他的例子以后将调用函数修改为:

 
 
  1. L?=?lua_open();?// luaL_newstate(); ?
  2. lua_baselibopen(L);?// ?luaL_openlibs(L); ?
  3. lua_dofile(L,?"e12.lua");//?luaL_dofile(L,?"e12.lua");?

然后编译出现以下错误:

/usr/bin/ld: cannot find -llualib
collect2: ld returned 1 exit status

又搜索了一些文档将编译方法修改为:

?g++ e13.cpp -o e13 -I/usr/local/include /usr/local/lib/liblua.a -llua -ldl

编译通过,执行:

./e13

The sum is 25

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读