【Lua】Windows下编译Lua引擎

本文详细介绍了在Windows环境下使用VS2019编译Lua5.3.5引擎的全过程,包括下载源码、编写批处理脚本、设置环境及编译步骤,最后验证编译成果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编译准备

下载源码

首先从官网上下载Lua 5.3.5的源代码到本地,并解压到合适的位置,我的目录结构如下:

D:\DEVELOPMENT\TOOLS\LUA
+---5.3.5
|   |   Makefile
|   |   README
|   |
|   +---doc
|   |       contents.html
|   |       index.css
|   |       logo.gif
|   |       lua.1
|   |       lua.css
|   |       luac.1
|   |       manual.css
|   |       manual.html
|   |       osi-certified-72x60.png
|   |       readme.html
|   |
|   \---src
|           *.c
|           *.h
|           *.hpp
|           Makefile

编写批处理脚本

install.bat

REM open `Developer Command Prompt For Visual Studio`
REM install.bat 5.3.5

%~d0
cd /d %~dp0%1\src

cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c
ren lua.obj lua.o
ren luac.obj luac.o
link /DLL /IMPLIB:lua%1.lib /OUT:lua%1.dll *.obj
link /OUT:lua.exe lua.o lua%1.lib
lib /OUT:lua%1-static.lib *.obj
link /OUT:luac.exe luac.o lua%1-static.lib

del *.obj
del *.o
if exist ..\bin (
    rd /s /q ..\bin
)
mkdir ..\bin
move *.exp ..\bin
move *.lib ..\bin
move *.dll ..\bin
move *.exe ..\bin

cd /d %~dp0

PAUSE

将install.bat文件置于5.3.5同级目录

开始编译

打开VS开发人员命令提示符

我安装了VS2019,所以我的机器上有一下的VS命令提示符程序:
VS命令提示符程序
根据个人要求选择其中一个打开即可(我选择x64那个吧)

执行批处理脚本

在install.bat统计目录编写另一个批处理文件install5.3.5.bat,内容简单一句即可:

%~dp0install.bat 5.3.5

将该文件拖到刚打开的命令提示符程序中,如下图:
执行install5.3.5.bat
Fire!
正常输出如下:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>D:\Development\Tools\Lua\install5.3.5.bat

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>D:\Development\Tools\Lua\install.bat 5.3.5

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>REM open `Developer Command Prompt For Visual Studio`

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>REM install.bat 5.3.5

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>D:

D:\>cd /d D:\Development\Tools\Lua\5.3.5\src

D:\Development\Tools\Lua\5.3.5\src>cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.22.27905 版
版权所有(C) Microsoft Corporation。保留所有权利。

lapi.c
lauxlib.c
lbaselib.c
lbitlib.c
lcode.c
lcorolib.c
lctype.c
ldblib.c
ldebug.c
ldo.c
ldump.c
lfunc.c
lgc.c
linit.c
liolib.c
llex.c
lmathlib.c
lmem.c
loadlib.c
lobject.c
正在生成代码...
正在编译...
lopcodes.c
loslib.c
lparser.c
lstate.c
lstring.c
lstrlib.c
ltable.c
ltablib.c
ltm.c
lua.c
luac.c
lundump.c
lutf8lib.c
lvm.c
lzio.c
正在生成代码...

D:\Development\Tools\Lua\5.3.5\src>ren lua.obj lua.o

D:\Development\Tools\Lua\5.3.5\src>ren luac.obj luac.o

D:\Development\Tools\Lua\5.3.5\src>link /DLL /IMPLIB:lua5.3.5.lib /OUT:lua5.3.5.dll *.obj
Microsoft (R) Incremental Linker Version 14.22.27905.0
Copyright (C) Microsoft Corporation.  All rights reserved.

  正在创建库 lua5.3.5.lib 和对象 lua5.3.5.exp

D:\Development\Tools\Lua\5.3.5\src>link /OUT:lua.exe lua.o lua5.3.5.lib
Microsoft (R) Incremental Linker Version 14.22.27905.0
Copyright (C) Microsoft Corporation.  All rights reserved.


D:\Development\Tools\Lua\5.3.5\src>lib /OUT:lua5.3.5-static.lib *.obj
Microsoft (R) Library Manager Version 14.22.27905.0
Copyright (C) Microsoft Corporation.  All rights reserved.


D:\Development\Tools\Lua\5.3.5\src>link /OUT:luac.exe luac.o lua5.3.5-static.lib
Microsoft (R) Incremental Linker Version 14.22.27905.0
Copyright (C) Microsoft Corporation.  All rights reserved.

  正在创建库 luac.lib 和对象 luac.exp

D:\Development\Tools\Lua\5.3.5\src>del *.obj

D:\Development\Tools\Lua\5.3.5\src>del *.o

D:\Development\Tools\Lua\5.3.5\src>if exist ..\bin (rd /s /q ..\bin )

D:\Development\Tools\Lua\5.3.5\src>mkdir ..\bin

D:\Development\Tools\Lua\5.3.5\src>move *.exp ..\bin
D:\Development\Tools\Lua\5.3.5\src\lua5.3.5.exp
D:\Development\Tools\Lua\5.3.5\src\luac.exp
移动了         2 个文件。

D:\Development\Tools\Lua\5.3.5\src>move *.lib ..\bin
D:\Development\Tools\Lua\5.3.5\src\lua5.3.5-static.lib
D:\Development\Tools\Lua\5.3.5\src\lua5.3.5.lib
D:\Development\Tools\Lua\5.3.5\src\luac.lib
移动了         3 个文件。

D:\Development\Tools\Lua\5.3.5\src>move *.dll ..\bin
D:\Development\Tools\Lua\5.3.5\src\lua5.3.5.dll
移动了         1 个文件。

D:\Development\Tools\Lua\5.3.5\src>move *.exe ..\bin
D:\Development\Tools\Lua\5.3.5\src\lua.exe
D:\Development\Tools\Lua\5.3.5\src\luac.exe
移动了         2 个文件。

D:\Development\Tools\Lua\5.3.5\src>cd /d D:\Development\Tools\Lua\

D:\Development\Tools\Lua>PAUSE
请按任意键继续. . .

结果

最终目录结构:

文件夹 PATH 列表
卷序列号为 42EB-4399
D:\DEVELOPMENT\TOOLS\LUA
|   install.bat
|   install5.3.5.bat
|
\---5.3.5
    |   Makefile
    |   README
    |
    +---bin
    |       lua.exe
    |       lua5.3.5-static.lib
    |       lua5.3.5.dll
    |       lua5.3.5.exp
    |       lua5.3.5.lib
    |       luac.exe
    |       luac.exp
    |       luac.lib
    |
    +---doc
    |       contents.html
    |       index.css
    |       logo.gif
    |       lua.1
    |       lua.css
    |       luac.1
    |       manual.css
    |       manual.html
    |       osi-certified-72x60.png
    |       readme.html
    |
    \---src
            *.c
            *.h
            *.hpp
            Makefile

可以看到多出了一个bin目录,将该目录放到path环境变量中,打开命令提示符程序,输入:

lua -v

or

luac -v

显示

Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio

即表示成功了!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值