详解LUA 5.1编译及实例操作

移动开发 iOS
因为在vs2005中lua是外来的,所以要让vs2005能找到lua的头文件和库文件(lua5.1中只有一个lua51.lib),具体内容来考哪本文详解。

LUA 5.1编译及实例操作是本文要介绍的内容,以前没接触过,并且能找的资料也少之又少, 花了两天的时间才搞定..一般的都是在vs2005中开发(我的就是),以下是关于vs2005中使用lua。

以下是我在vs2005编译方法:

1、打开vs命令行工具、   工具->visual studio 2005 command prompt

2、到lua-5.1的目录也就是etc的上一级目录    :cd/d d:\lua-5.1

3、执行:etc\luavs.bat(注意:这里是\,不是/)

4、然后lua51.dll ,lua51.lib,lua.exe就生成在src路径下了~(注意因为是lua5.1的所以没有luac.exe,以前的版本有)

因为在vs2005中lua是外来的,所以要让vs2005能找到lua的头文件和库文件(lua5.1中只有一个lua51.lib),在vs中包含,于是:工具->选项->项目和解决方案->vc++ 目录  右边选择包含文件把src的路径包含进来(关于头文件的)。

还有库文件同意的操作,不过这里就是后来一直出错的点,这里这种方式包含的库文件是包含不进来的,后面讲到的一种方法可正确包含

头文件和库文件都包含进来后就可以在c++中使用lua了

看一个实例如下:

  1. #include <stdio.h> 
  2. #include <iostream> 
  3. extern "C"  
  4. {  
  5.    #include "lua.h"  
  6.    #include "lualib.h"  
  7.    #include "lauxlib.h"  
  8. }//在工具中包含文件的那个~~  
  9. /* Lua解释器指针 */  
  10. lua_State * L;  
  11. #pragma comment(lib,"lua51.lib")//包含库文件~~在工具里包含不进来,上面的包含不进来的处理办法是:  
  12. //把lua51.dll 拷到项目的dubug目录下,把lua51.lib拷到项目目录的项目名的文件夹下  
  13. int main ()  
  14. {  
  15.  /* 初始化Lua */  
  16.  L = lua_open();  
  17.  /* 载入Lua基本库 */  
  18.  luaL_openlibs(L);  
  19.  /* 运行脚本 ,注意路径*/  
  20.  luaL_dofile(L, "d:\\test.lua");  
  21.  /* 清除Lua */  
  22.  lua_close(L);  
  23.  //printf( "Press enter to exit…" );  
  24.  //getchar();  
  25.  return 0;  

上面是c++的一个空工程

下面是test.lua的代码:是一个石头剪子布的小的游戏实例

代码如下:

  1. ---[[  
  2. math.randomseed(os.time()) --[[为随机数产生器生成一个种子--]]  
  3. user_score = 0 
  4. comp_score = 0  -- 全局变量存分数  
  5. lookup = {};     --输赢对照表  
  6. lookup["rock"]={rock = "draw",paper = "lose",scissors ="win"}  
  7. lookup["paper"]={rock = "win",paper = "draw",scissors = "lose"}  
  8. lookup["scissors"]={rock = "lose",paper = "win",scissors = "draw"}  
  9. function GetAiMove()  --Ai的函数  
  10.   local int_to_string = {"rock","paper","scissors"}  --局部一个table,对照用  
  11.   return int_to_string[math.random(3)]  
  12. end  
  13. function EvaluateTheGuess(user_guess,comp_guess)   -- 计算结果的函数  
  14.   if(lookup[user_guess][comp_guess]=="win") then  
  15.      print ("user win the game")  
  16.      --print()  
  17.      user_scoreuser_score=user_score+1         --小错误 ~已改  
  18.   elseif (lookup[user_guess][comp_guess]=="lose") then  
  19.      print ("user lose the game")  
  20.      --print()  
  21.      comp_scorecomp_score=comp_score+1  
  22.   else  
  23.       print ("draw!")  
  24.       --print()  
  25.   end  
  26. end 

下面开始

  1. print ("game begin:enter q to guit game")  
  2. --print() --换行?  
  3. loop = true 
  4. while loop==true do  
  5.   --print()  
  6.   print("user: "..user_score.." comp: "..comp_score)  
  7.   print()  
  8.   print("p--布  r--拳头  s--减")  
  9.   print("请输入:")  
  10.   --io.open()  
  11.   user_guess =io.stdin:read '*l'    --出错的地方,这里是l不是1  
  12.   --user_guess = "r" 
  13.   print()  
  14.   local letter_to_string = {r="rock",s="scissors",p="paper"}    --亦是局部的一个table 对照用的  
  15.   if(user_guess == "q") then  
  16.      loop = false 
  17.   elseif(user_guess == "r") or (user_guess == "s") or(user_guess =="p")  then  
  18.      comp_guess=GetAiMove()  
  19.      EvaluateTheGuess(letter_to_string[user_guess],comp_guess)  
  20.   else  
  21.      print ("invalid input,try again")  
  22.   end  
  23. end  
  24. --]] 

小结:关于详解LUA 5.1编译及实例操作的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-25 15:21:02

Lua字符串

2011-08-24 15:28:02

Lua编译器解释器

2009-08-20 11:01:51

C#操作内存

2011-06-29 11:06:12

Qt Qvfb

2011-07-05 14:29:44

Ubuntu QT Mysql

2011-08-23 17:06:03

2012-05-08 11:01:45

linux守护进程

2009-09-07 05:50:59

C# Timer用法

2013-01-15 15:18:46

Linux守护进程

2009-08-18 17:05:08

C#操作xml文件

2011-07-04 15:13:31

QT MPlayer 移植

2011-08-23 16:48:41

Lua 5.1API 函数

2011-07-06 16:25:10

iPhone 程序 调用

2011-07-19 13:20:22

Xcode

2009-12-24 09:16:11

C#泛型

2019-09-12 08:32:40

Linuxsed命令语法

2011-07-26 18:11:56

iPhone Sqlite 数据库

2010-01-13 10:14:45

2009-08-26 14:03:26

C#打印原理

2011-09-06 17:45:14

LUA开发环境Decoda
点赞
收藏

51CTO技术栈公众号