## 下載 Lua 源碼
[Lua 所有版本的源碼](http://www.lua.org/versions.html)
本例使用的是 [Lua 5.1.5](http://www.lua.org/ftp/lua-5.1.5.tar.gz)
下載解壓后的目錄結構如下:
```text
doc
etc
src
test
COPYRIGHT
HISTORY
INSTALL
Makefile
README
```
## Lua 安裝
[Lua 各平臺的快捷安裝方式](http://www.runoob.com/lua/lua-environment.html)
## 其他
### 閱讀 Lua 官方 ReadMe
如果感興趣或者安裝步驟上出現問題,可閱讀 [Lua 官網 ReadMe](http://www.lua.org/manual/5.3/readme.html),了解編譯 Lua 的詳細步驟
[Lua 官方 ReadMe 中文版](https://cloudwu.github.io/lua53doc/)
### Windows 上編譯 Lua
我研究了兩種編譯方式 `利用 VS 編譯` 和 `利用 MinGW 編譯`
由于之后利用 VS 斷點調試研究 Lua 源碼是我的主要手段
故以下先介紹 `利用 VS 編譯`
#### 利用 VS 編譯
主要參考了 [Windows7 下 Lua 的編譯和配置](http://blog.csdn.net/linkhai/article/details/45568853),略作改動
**新建解決方案**
以 VS 2015 為例
依次點擊菜單 `文件 -> 新建 -> 項目`
依次選擇 `模板 -> 其他項目類型 -> Visual Studio 解決方案`
名稱填 `Lua51`
位置假設填 `X:`
點擊 `確定`
**新建項目**
把解壓的 Lua 源碼的 src 目錄復制到 `X:\Lua51` 下
刪除 src 下的 `Makefile` 文件
在 VS 中,右擊 `解決方案資源管理器` 下的解決方案
依次點擊菜單 `添加 -> 新建項目`
依次選擇 `Visual C++ -> 空項目`
名稱填 `lua51`
點擊 `確定`
按照以上新建項目的步驟再新建兩個分別名為 `lua` 和 `luac` 的項目
**添加文件**
右擊 `lua51` 項目,依次點擊菜單 `添加->現有項`
選擇 `X:\Lua51\src` 下除了 `lua.c` 和 `luac.c` 外的所有文件
點擊 `添加`
`lua` 項目添加 `lua.c`
`luac` 項目添加 `luac.c`
步驟同上
**配置**
右擊 `lua51` 項目,選擇 `屬性` 菜單
依次選擇 `配置屬性 -> C/C++ -> 預處理器 -> 預處理器定義`
添加 `_CRT_SECURE_NO_DEPRECATE`
`lua` 和 `luac` 項目的操作步驟同上
否則會在生成時報如以下的此類錯誤:
```text
...
錯誤 C4996 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
...
```
右擊 `lua51` 項目,`屬性 -> 配置屬性 -> 常規 -> 配置類型`
選擇 `靜態庫`
點擊 `應用`
點擊 `確定`
`lua` 項目,`屬性 -> 配置屬性 -> 鏈接器 -> 常規 -> 附加庫目錄` ,添加 `$(OutDir)`
`屬性 -> 配置屬性 -> 鏈接器 -> 輸入 -> 附加依賴項` ,添加 `lua51.lib`
點擊 `應用`
點擊 `確定`
`luac` 項目同上
**生成**
由于 `lua` 和 `luac` 項目依賴 `lua51` 生成的 `lua51.lib`
所以首先得生成 `lua51` ,再生成另兩個項目
所有項目都生成完成后,會在輸出目錄下輸出解釋器 lua.exe 和編譯器 luac.exe
#### 利用 MinGW 編譯
**安裝 MinGW**
下載并安裝 [MinGW](https://sourceforge.net/projects/mingw/files/latest/download?source=files)
安裝完成后勾選 `Baseic Setup` 中的除了 `mingw32-gcc-objc` 的所有選項
點擊 `Apply Changes` 按鈕
等待安裝完成后即可關閉 `MinGW Installation Manager`
在 `環境變量`->`系統變量` 中的 `Path` 變量中添加 `x:\MinGW\bin`
即添加 `MinGW` 安裝目錄下的 `bin` 目錄路徑
打開 Lua 源碼目錄下的 `src/Makefile` 文件
找到并刪除字符串 `# DLL needs all object files`
否則會在 make 時報以下錯誤:
```text
...
gcc -shared -o lua51.dll lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o
ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o loadlib.o linit.o # DLL needs all object files
gcc: error: #: No such file or directory
gcc: error: DLL: No such file or directory
gcc: error: needs: No such file or directory
gcc: error: all: No such file or directory
gcc: error: object: No such file or directory
gcc: error: files: No such file or directory
Makefile:51: recipe for target 'lua51.dll' failed
mingw32-make[2]: *** [lua51.dll] Error 1
mingw32-make[2]: Leaving directory 'X:/lua-5.1.5/src'
Makefile:107: recipe for target 'mingw' failed
mingw32-make[1]: *** [mingw] Error 2
mingw32-make[1]: Leaving directory 'X:/lua-5.1.5/src'
Makefile:56: recipe for target 'mingw' failed
mingw32-make: *** [mingw] Error 2
```
回到 Lua 源碼目錄,`Shift`+`右鍵` 彈出菜單列表
選擇 `在此處打開命令窗口`
在命令窗口中輸入 `mingw32-make mingw`
這里參照了 [Lua 官網 ReadMe](http://www.lua.org/manual/5.3/readme.html) 中所提及的 `make xxx` 命令
`xxx` 為平臺名,Makefile 提供的平臺名有:
```text
aix
ansi
bsd
freebsd
generic
linux
macosx
mingw
posix
solaris
```
等待運行完成后,在 `src` 目錄下已生成各種 .o 文件及解釋器 lua.exe 和 編譯器 luac.exe
此時,若前面沒有修改 `src/Makefile` 則會報以下錯誤:
```text
...
gcc -shared -o lua51.dll lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o
ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o loadlib.o linit.o # DLL needs all object files
gcc: error: #: No such file or directory
gcc: error: DLL: No such file or directory
gcc: error: needs: No such file or directory
gcc: error: all: No such file or directory
gcc: error: object: No such file or directory
gcc: error: files: No such file or directory
Makefile:51: recipe for target 'lua51.dll' failed
mingw32-make[2]: *** [lua51.dll] Error 1
mingw32-make[2]: Leaving directory 'X:/lua-5.1.5/src'
Makefile:107: recipe for target 'mingw' failed
mingw32-make[1]: *** [mingw] Error 2
mingw32-make[1]: Leaving directory 'X:/lua-5.1.5/src'
Makefile:56: recipe for target 'mingw' failed
mingw32-make: *** [mingw] Error 2
```
接著參照類 Unix 平臺上的安裝到系統默認目錄的命令 `make xxx install`
在命令窗口輸入 `mingw32-make mingw install`
結果報錯:
```text
cd src && mingw32-make mingw
mingw32-make[1]: Entering directory 'X:/lua-5.1.5/src'
mingw32-make "LUA_A=lua51.dll" "LUA_T=lua.exe" \
"AR=gcc -shared -o" "RANLIB=strip --strip-unneeded" \
"MYCFLAGS=-DLUA_BUILD_AS_DLL" "MYLIBS=" "MYLDFLAGS=-s" lua.exe
mingw32-make[2]: Entering directory 'X:/lua-5.1.5/src'
mingw32-make[2]: 'lua.exe' is up to date.
mingw32-make[2]: Leaving directory 'X:/lua-5.1.5/src'
mingw32-make "LUAC_T=luac.exe" luac.exe
mingw32-make[2]: Entering directory 'X:/lua-5.1.5/src'
mingw32-make[2]: 'luac.exe' is up to date.
mingw32-make[2]: Leaving directory 'X:/lua-5.1.5/src'
mingw32-make[1]: Leaving directory 'X:/lua-5.1.5/src'
cd src && mkdir -p /usr/local/bin /usr/local/include /usr/local/lib /usr/local/man/man1 /usr/local/share/lua/5.1 /usr/local/lib/lua/5.1
命令語法不正確。
Makefile:62: recipe for target 'install' failed
mingw32-make: *** [install] Error 1
```
因為在 Windows 下,mkdir 的多層級路徑得用 符號分隔
如果想要順利安裝,可修改 Lua 源碼目錄下的 Makefile
不過在 Windows 下已經生成可用的 lua.exe 和 luac.exe
沒有必要強求完成 `make xxx install` 這一步
故在此不贅述修改步驟