一、官网源码url:
 
二、windows平台环境搭建:
方式一(可视化新建vs项目生成):
(1)生成lua库文件
1.官网上下载lua源码
2.用vs新建win32静态库项目(去选预编译头,本人的项目命名为“lua”)
3.添加源码中所有头文件和除lua.c、luac.c以外的源文件到项目中。
4.选择release模式,生成->重新生成解决方案。
(2)生成lua解释器
1.用vs新建win32控制台项目(勾选空项目,本人的项目项目命名为“lua”)
2.添加lua.c到项目中
3.添加lua库的引用,右键项目->属性->配置属性->链接器->输入->附加依赖项,添加刚生成的lua库的路径。
4.选择release模式,生成项目。
(3)生成lua编译器
1.用vs新建win32控制台项目(勾选空项目,本人的项目项目命名为“luac”)
2.添加luac.c到项目中
3.添加lua库的引用,右键项目->属性->配置属性->链接器->输入->附加依赖项,添加刚生成的lua库的路径。
4.选择release模式,生成项目。
上面是同一个解决方案下的三个项目,最终Release目录下会生成lua.lib、lua.exe和luac.exe
直接使用lua.exe便可敲起你的测试代码。
 
(4)添加系统环境变量
把lua.lib、lua.exe和luac.exe统一到一个目录bin下
右键我的电脑->属性->高级系统设置->环境变量,找到Path变量进行编辑,添加刚才的bin目录。
运行cmd测试下
 
方式二(通过vs的cl命令生成):
(1)准备c/c++命令行编译环境
1.检查cl是否已生效。
若没cl命令,就自己弄一个。
首先安装一个vs(本人的是10版本)
cmd下,vs安装目录\Common7\Tools,执行vsvars32.bat脚本进行加载vs环境变量
注意:
仅限于本次命令行示例,在启动cmd就不生效了。
若失败了,顺着脚本看下问题。常见的是系统环境变量没有vs特定的宏。
VS2003的宏是“VS71COMNTOOLS”
VS2005的宏是“VS80COMNTOOLS”
VS2008的宏是“VS90COMNTOOLS”
VS2010的宏是“VS100COMNTOOLS”
解决方案:系统环境变量添加一个VS100COMNTOOLS,值为"vs安装目录\Common7\Tools"(没有“;”)。
然后敲下cl来检查命令是否生效。
 
(2)bat脚本生成lua库、解释器和编译器
如果lua源码目录下有etc\luavs.bat脚本(内容在附录中5.1.4版本所示)则执行它生成lua库、解释器和编译器
注意:脚本中的注意项
@rem Script to build Lua under "Visual Studio .NET Command Prompt".
该脚本依赖vs命令行环境
@rem Do not run from this directory; run it from the toplevel: etc\luavs.bat .
需要在etc文件夹所在的目录上执行该脚本
@rem It creates lua51.dll, lua51.lib, lua.exe, and luac.exe in src.
该脚本会在src目录下创建 lua51.dll, lua51.lib, lua.exe, and luac.exe
执行完后会在src目录下生成lua库、解释器和编译器。
若没有etc\luavs.bat就自己添加一个(内容在附录中更改后的luavs.bat所示)。
注意:
1.中途可能会报一个print.c文件无法打开的问题(这个问题不影响lua库、解释器和编译器的生产,想解决的就自己添加一个print.c(内容在附录中,且不同版本很可能会编译不成功),个人推荐直接删除编译print.c的脚本(下面红框所标))
2.添加编译luac.exe脚本
 
三、附录
5.1.4版本的luavs.bat脚本内容如下:
@rem Script to build Lua under "Visual Studio .NET Command Prompt".
@rem Do not run from this directory; run it from the toplevel: etc\luavs.bat .
@rem It creates lua51.dll, lua51.lib, lua.exe, and luac.exe in src.
@rem (contributed by David Manura and Mike Pall)
 
@setlocal
@set MYCOMPILE=cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE
@set MYLINK=link /nologo
@set MYMT=mt /nologo
 
cd src
%MYCOMPILE% /DLUA_BUILD_AS_DLL l*.c
del lua.obj luac.obj
%MYLINK% /DLL /out:lua51.dll l*.obj
if exist lua51.dll.manifest^
%MYMT% -manifest lua51.dll.manifest -outputresource:lua51.dll;2
%MYCOMPILE% /DLUA_BUILD_AS_DLL lua.c
%MYLINK% /out:lua.exe lua.obj lua51.lib
if exist lua.exe.manifest^
%MYMT% -manifest lua.exe.manifest -outputresource:lua.exe
%MYCOMPILE% l*.c print.c
del lua.obj linit.obj lbaselib.obj ldblib.obj liolib.obj lmathlib.obj^
loslib.obj ltablib.obj lstrlib.obj loadlib.obj
%MYLINK% /out:luac.exe *.obj
if exist luac.exe.manifest^
%MYMT% -manifest luac.exe.manifest -outputresource:luac.exe
del *.obj *.manifest
cd ..
 
更改后的luavs.bat脚本内容如下:
@rem Script to build Lua under "Visual Studio .NET Command Prompt".
@rem Do not run from this directory; run it from the toplevel: etc\luavs.bat .
@rem It creates lua51.dll, lua51.lib, lua.exe, and luac.exe in src.
@rem (contributed by David Manura and Mike Pall)
 
@setlocal
@set MYCOMPILE=cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE
@set MYLINK=link /nologo
@set MYMT=mt /nologo
 
cd src
%MYCOMPILE% /DLUA_BUILD_AS_DLL l*.c
del lua.obj luac.obj
%MYLINK% /DLL /out:lua51.dll l*.obj
if exist lua51.dll.manifest^
%MYMT% -manifest lua51.dll.manifest -outputresource:lua51.dll;2
%MYCOMPILE% /DLUA_BUILD_AS_DLL lua.c
%MYLINK% /out:lua.exe lua.obj lua51.lib
if exist lua.exe.manifest^
%MYMT% -manifest lua.exe.manifest -outputresource:lua.exe
del lua.obj linit.obj lbaselib.obj ldblib.obj liolib.obj lmathlib.obj^
loslib.obj ltablib.obj lstrlib.obj loadlib.obj
%MYCOMPILE% /DLUA_BUILD_AS_DLL luac.c
%MYLINK% /out:luac.exe *.obj lua51.lib
if exist luac.exe.manifest^
%MYMT% -manifest luac.exe.manifest -outputresource:luac.exe
del *.obj *.manifest
cd ..
 
 
5.1.4版本的print.c:
/*
** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
** print bytecodes
** See Copyright Notice in lua.h
*/
 
#include <ctype.h>
#include <stdio.h>
 
#define luac_c
#define LUA_CORE
 
#include "ldebug.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lundump.h"
 
#define PrintFunction luaU_print
 
#define Sizeof(x) ((int)sizeof(x))
#define VOID(p) ((const void*)(p))
 
static void PrintString(const TString* ts)
{
const char* s=getstr(ts);
size_t i,n=ts->tsv.len;
putchar('"');
for (i=0; i<n; i++)
{
int c=s[i];
switch (c)
{
case '"': printf("\\\""); break;
case '\\': printf("\\\\"); break;
case '\a': printf("\\a"); break;
case '\b': printf("\\b"); break;
case '\f': printf("\\f"); break;
case '\n': printf("\\n"); break;
case '\r': printf("\\r"); break;
case '\t': printf("\\t"); break;
case '\v': printf("\\v"); break;
default: if (isprint((unsigned char)c))
putchar(c);
else
printf("\\%03u",(unsigned char)c);
}
}
putchar('"');
}
 
static void PrintConstant(const Proto* f, int i)
{
const TValue* o=&f->k[i];
switch (ttype(o))
{
case LUA_TNIL:
printf("nil");
break;
case LUA_TBOOLEAN:
printf(bvalue(o) ? "true" : "false");
break;
case LUA_TNUMBER:
printf(LUA_NUMBER_FMT,nvalue(o));
break;
case LUA_TSTRING:
PrintString(rawtsvalue(o));
break;
default: /* cannot happen */
printf("? type=%d",ttype(o));
break;
}
}
 
static void PrintCode(const Proto* f)
{
const Instruction* code=f->code;
int pc,n=f->sizecode;
for (pc=0; pc<n; pc++)
{
Instruction i=code[pc];
OpCode o=GET_OPCODE(i);
int a=GETARG_A(i);
int b=GETARG_B(i);
int c=GETARG_C(i);
int bx=GETARG_Bx(i);
int sbx=GETARG_sBx(i);
int line=getline(f,pc);
printf("\t%d\t",pc+1);
if (line>0) printf("[%d]\t",line); else printf("[-]\t");
printf("%-9s\t",luaP_opnames[o]);
switch (getOpMode(o))
{
case iABC:
printf("%d",a);
if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
break;
case iABx:
if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx);
break;
case iAsBx:
if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
break;
}
switch (o)
{
case OP_LOADK:
printf("\t; "); PrintConstant(f,bx);
break;
case OP_GETUPVAL:
case OP_SETUPVAL:
printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-");
break;
case OP_GETGLOBAL:
case OP_SETGLOBAL:
printf("\t; %s",svalue(&f->k[bx]));
break;
case OP_GETTABLE:
case OP_SELF:
if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
break;
case OP_SETTABLE:
case OP_ADD:
case OP_SUB:
case OP_MUL:
case OP_DIV:
case OP_POW:
case OP_EQ:
case OP_LT:
case OP_LE:
if (ISK(b) || ISK(c))
{
printf("\t; ");
if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
printf(" ");
if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
}
break;
case OP_JMP:
case OP_FORLOOP:
case OP_FORPREP:
printf("\t; to %d",sbx+pc+2);
break;
case OP_CLOSURE:
printf("\t; %p",VOID(f->p[bx]));
break;
case OP_SETLIST:
if (c==0) printf("\t; %d",(int)code[++pc]);
else printf("\t; %d",c);
break;
default:
break;
}
printf("\n");
}
}
 
#define SS(x) (x==1)?"":"s"
#define S(x) x,SS(x)
 
static void PrintHeader(const Proto* f)
{
const char* s=getstr(f->source);
if (*s=='@' || *s=='=')
s++;
else if (*s==LUA_SIGNATURE[0])
s="(bstring)";
else
s="(string)";
printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
(f->linedefined==0)?"main":"function",s,
f->linedefined,f->lastlinedefined,
S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
f->numparams,f->is_vararg?"+":"",SS(f->numparams),
S(f->maxstacksize),S(f->nups));
printf("%d local%s, %d constant%s, %d function%s\n",
S(f->sizelocvars),S(f->sizek),S(f->sizep));
}
 
static void PrintConstants(const Proto* f)
{
int i,n=f->sizek;
printf("constants (%d) for %p:\n",n,VOID(f));
for (i=0; i<n; i++)
{
printf("\t%d\t",i+1);
PrintConstant(f,i);
printf("\n");
}
}
 
static void PrintLocals(const Proto* f)
{
int i,n=f->sizelocvars;
printf("locals (%d) for %p:\n",n,VOID(f));
for (i=0; i<n; i++)
{
printf("\t%d\t%s\t%d\t%d\n",
i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
}
}
 
static void PrintUpvalues(const Proto* f)
{
int i,n=f->sizeupvalues;
printf("upvalues (%d) for %p:\n",n,VOID(f));
if (f->upvalues==NULL) return;
for (i=0; i<n; i++)
{
printf("\t%d\t%s\n",i,getstr(f->upvalues[i]));
}
}
 
void PrintFunction(const Proto* f, int full)
{
int i,n=f->sizep;
PrintHeader(f);
PrintCode(f);
if (full)
{
PrintConstants(f);
PrintLocals(f);
PrintUpvalues(f);
}
for (i=0; i<n; i++) PrintFunction(f->p[i],full);
}