choco install lua51
会安装x86版本的lua5.1版本
打开x64 Native Tools Command Prompt 输入
dumpbin /headers lua.exe
可以查看是x86版本的
输入
dumpin /dependents lua.exe
可以看到是依赖Visual Studio 2005版本编译的。
choco 安装lua提供了很多clibs,而且自带LuaRocks。x86的luarocks默认找到choco的x64 gcc 编译器,这个需要手动安装Visual Studio 2005版本然后让LuaRocks选择VS2005的x86的cl编译器当作LuaRocks的默认编译器,来安装很多Lua库。
在windows下主要还是MSVC占主导地位,当然如果你选择MinGW x64,那也无可厚非,只要源码方便能够正常编译即可。
1.操作系统 Win10 x64
2.Visual Studio 2019
官网下载lua5.1.5的源码包,解压打开发现里面只有makefile可以用于编译。
添加如下文件内容到CMakeLists.txt,放在lua源码根目录下
project ( lua51 )
cmake_minimum_required ( VERSION 2.8 )
include_directories ( src ${CMAKE_CURRENT_BINARY_DIR} )
set ( SRC_CORE src/lapi.c src/lcode.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c src/lgc.c src/llex.c
src/lmem.c src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c src/lstring.c src/ltable.c
src/ltm.c src/lundump.c src/lvm.c src/lzio.c )
set ( SRC_LIB src/lauxlib.c src/lbaselib.c src/ldblib.c src/liolib.c
src/lmathlib.c src/loslib.c src/lstrlib.c src/ltablib.c src/loadlib.c src/linit.c )
set ( SRC_LUA src/lua.c )
set ( SRC_LUAC src/luac.c src/print.c )
add_library ( liblua ${SRC_CORE} ${SRC_LIB} )
set_target_properties ( liblua PROPERTIES OUTPUT_NAME lua )
add_library ( libluadll SHARED ${SRC_CORE} ${SRC_LIB} )
target_compile_definitions ( libluadll PRIVATE _CRT_SECURE_NO_WARNINGS LUA_BUILD_AS_DLL )
set_target_properties ( libluadll PROPERTIES OUTPUT_NAME lua51 )
add_executable ( lua ${SRC_LUA} )
target_link_libraries ( lua libluadll )
add_executable ( luac ${SRC_LUAC} )
target_link_libraries ( luac liblua )
cmake工程会生成 lua.exe lua51.dll lua51.lib luac.exe四个文件
添加如下内容到msvcbuild.bat,放在lua源码根目录下
@echo off
if "%ARCH%" equ "" (
if %PROCESSOR_ARCHITECTURE%==x86 (
set ARCH=x86
) else (
set ARCH=x64
)
)
if "%VS%"=="2019" (set VERSION=16) && goto :generator
if "%VS%"=="16" (set VERSION=16) && goto :generator
if "%VS%"=="2017" (set VERSION=15) && goto :generator
if "%VS%"=="15" (set VERSION=15) && goto :generator
if "%VS%"=="2015" (set VERSION=14) && goto :generator
if "%VS%"=="14" (set VERSION=14) && goto :generator
if "%VS%"=="2013" (set VERSION=12) && goto :generator
if "%VS%"=="12" (set VERSION=12) && goto :generator
if "%VS%"=="2012" (set VERSION=11) && goto :generator
if "%VS%"=="11" (set VERSION=11) && goto :generator
if "%VS%"=="2010" (set VERSION=10) && goto :generator
if "%VS%"=="10" (set VERSION=10) && goto :generator
if "%VS%"=="2008" (set VERSION=9) && goto :generator
if "%VS%"=="9" (set VERSION=9) && goto :generator
if "%VS%"=="2005" (set VERSION=8) && goto :generator
if "%VS%"=="8" (set VERSION=8) && goto :generator
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.16.0" >nul 2>nul
if %errorlevel%==0 (set VERSION=16) && goto :generator
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.15.0" >nul 2>nul
if %errorlevel%==0 (set VERSION=15) && goto :generator
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.14.0" >nul 2>nul
if %errorlevel%==0 (set VERSION=14) && goto :generator
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.12.0" >nul 2>nul
if %errorlevel%==0 (set VERSION=12) && goto :generator
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.10.0" >nul 2>nul
if %errorlevel%==0 (set VERSION=11) && goto :generator
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.10.0" >nul 2>nul
if %errorlevel%==0 (set VERSION=10) && goto :generator
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.9.0 ">nul 2>nul
if %errorlevel%==0 (set VERSION=9) && goto :generator
reg query "HKEY_CLASSES_ROOT\VisualStudio.DTE.8.0 ">nul 2>nul
if %errorlevel%==0 (set VERSION=8) && goto :generator
echo "Visual Studio is not installed"
goto :clean
:generator
set ARG_GENERATOR=-G"Visual Studio %VERSION%"
if %VERSION% leq 15 (
if "%ARCH%" equ "x86" set ARG_ARCH="" && goto :build
if "%ARCH%" equ "x64" set ARG_GENERATOR=%ARG_GENERATOR%" Win64" && set ARG_ARCH="" && goto :build
) else (
if "%ARCH%" equ "x86" set ARG_ARCH=-AWin32 && goto :build
if "%ARCH%" equ "x64" set ARG_ARCH=-Ax64 && goto :build
)
echo "Windows ARCH(%ARCH%) is invalid"
goto :clean
:build
if %errorlevel%==0 (cmake -H. -Bbuild %ARG_GENERATOR% %ARG_ARCH%)
if %errorlevel%==0 (cmake --build build --config Release)
goto :install
:install
md Lua\5.1 Lua\5.1\include Lua\5.1\lib
copy /Y build\Release\lua.exe Lua\5.1\lua.exe
copy /Y build\Release\luac.exe Lua\5.1\luac.exe
copy /Y build\Release\lua51.dll Lua\5.1\lua5.1.dll
copy /Y build\Release\lua51.dll Lua\5.1\lua51.dll
copy /Y build\Release\lua51.lib Lua\5.1\lib\lua5.1.lib
copy /Y build\Release\lua51.dll Lua\5.1\lib\lua5.1.dll
copy /Y build\Release\lua51.lib Lua\5.1\lib\lua51.lib
copy /Y build\Release\lua51.dll Lua\5.1\lib\lua51.dll
copy /Y src\lauxlib.h Lua\5.1\include\lauxlib.h
copy /Y src\lua.h Lua\5.1\include\lua.h
copy /Y src\luaconf.h Lua\5.1\include\luaconf.h
copy /Y src\lualib.h Lua\5.1\include\lualib.h
copy /Y etc\lua.hpp Lua\5.1\include\lua.hpp
explorer Lua\5.1
goto :end
:clean
if EXIST build RMDIR /S /Q build
if EXIST build RMDIR /S /Q Lua
goto :end
:end
双击msvcbuild.bat 进行编译,脚本会识别msvc编译器,然后把编译的二进制和一部分头文件梳理到新的目录中
拷贝到当前目录的Lua目录到 C:\Program Files\Lua
或者拷贝5.1目录到C:\Program Files\Lua\5.1
需要把Lua安装目录放在其他Lua版本的前面,当CMake在编译其他扩展库时,如果查找到了系统PATH的路径,则可以连接到正确的lua版本。
图中红框内的路径为Lua5.1 (x64) 的安装路径,下面的x86版本为choco安装的版本,将x64版本置于x86版本之上,以编译x64版本的扩展库。
where lua
返回
C:\Program Files\Lua\5.1\lua.exe
C:\Program Files (x86)\Lua\5.1\lua.exe
正常
官网 下载luarocks-3.3.1-win32.zip (legacy Windows package, includes Lua 5.1)
在根目录下使用cmd执行以下命令
install /MSVC /P "C:\Program Files\LuaRocks\5.1" /TREE "C:\Program Files\LuaRocks\5.1" /SCRIPTS "C:\Program Files\LuaRocks\5.1\scripts" /LUAMOD "C:\Program Files\LuaRocks\5.1\lua" /CMOD "C:\Program Files\LuaRocks\5.1\clibs" /LV 5.1 /LUA "C:\Program Files\Lua\5.1" /INC "C:\Program Files\Lua\5.1\include" /LIB "C:\Program Files\Lua\5.1\lib" /BIN "C:\Program Files\Lua\5.1"
检查安装环境
==========================
== System check results ==
==========================
Will configure LuaRocks with the following paths:
LuaRocks : C:\Program Files\LuaRocks\5.1
Config file : C:\Program Files\LuaRocks\5.1\config-5.1.lua
Rocktree : C:\Program Files\LuaRocks\5.1
Lua interpreter : C:\Program Files\Lua\5.1\lua.exe
binaries : C:\Program Files\Lua\5.1
libraries : C:\Program Files\Lua\5.1\lib
includes : C:\Program Files\Lua\5.1\include
architecture: x86_64
binary link : lua5.1.lib with runtime VCRUNTIME140.dll
Compiler : Microsoft (make sure it is in your path before using LuaRocks)
Press <ENTER> to start installing, or press <CTRL>+<C> to abort. Use install /? for installation options
按Enter进入安装
安装完成后会弹出一下内容
== LuaRocks is installed! ==
============================
You may want to add the following elements to your paths;
Lua interpreter;
PATH : C:\Program Files\Lua\5.1
PATHEXT : .LUA
LuaRocks;
PATH : C:\Program Files\LuaRocks\5.1
LUA_PATH : C:\Program Files\LuaRocks\5.1\lua\?.lua;C:\Program Files\LuaRocks\5.1\lua\?\init.lua
Local user rocktree (Note: %APPDATA% is user dependent);
PATH : %APPDATA%\LuaRocks\bin
LUA_PATH : %APPDATA%\LuaRocks\share\lua\5.1\?.lua;%APPDATA%\LuaRocks\share\lua\5.1\?\init.lua
LUA_CPATH: %APPDATA%\LuaRocks\lib\lua\5.1\?.dll
System rocktree
PATH : C:\Program Files\LuaRocks\5.1\scripts
LUA_PATH : C:\Program Files\LuaRocks\5.1\lua\?.lua;C:\Program Files\LuaRocks\5.1\lua\?\init.lua
LUA_CPATH: C:\Program Files\LuaRocks\5.1\clibs\?.dll
Note that the %APPDATA% element in the paths above is user specific and it MUST be replaced by its actual value.
For the current user that value is: C:\Users\lzong\AppData\Roaming.
这些变量中的路径不要随意添加,可能会干扰Lua调试器的自动运行。
LuaRocks安装的库会出现在如下位置
System rocktree
PATH : C:\Program Files\LuaRocks\5.1\scripts
LUA_PATH : C:\Program Files\LuaRocks\5.1\lua\?.lua;C:\Program Files\LuaRocks\5.1\lua\?\init.lua
LUA_CPATH: C:\Program Files\LuaRocks\5.1\clibs\?.dll
将
C:\Program Files\LuaRocks\5.1\lua?.lua;C:\Program Files\LuaRocks\5.1\lua?\init.lua
添加到package.path中
将
C:\Program Files\LuaRocks\5.1\clibs?.dll
添加到package.cpath中。
添加LuaRocks路径到PATH环境变量
C:\Program Files\LuaRocks\5.1
需要把LuaRocks安装目录放在其他LuaRocks版本的前面
where luarocks
返回
C:\Program Files\LuaRocks\5.1\luarocks.bat
vcpkg的使用参照这里
triplet选择 x64-windows
打开cmd执行
luarocks install luabitop
执行报错,
'mingw32-gcc' is not recognized as an internal or external command,
operable program or batch file.
注意安装时前面有这句话
Compiler : Microsoft (make sure it is in your path before using LuaRocks)
意思时在LuaRocks之前没有找到vs编译器
于是以管理员权限打开x64 Native Tools Command Prompt for VS 2019.bat 会自动添加所有运行程序到环境变量,而不会污染全局环境,
然后再次运行
luabitop 1.0.2-3 depends on lua >= 5.1, < 5.3 (5.1-1 provided by VM)
cl /nologo /MD /O2 -c -Fobit.obj -IC:\Program Files\Lua\5.1\include bit.c
bit.c
link -dll -def:bit.def -out:bit.dll C:\Program Files\Lua\5.1/lua51.lib bit.obj
Microsoft (R) Incremental Linker Version 14.25.28610.4
Copyright (C) Microsoft Corporation. All rights reserved.
Creating library bit.lib and object bit.exp
luabitop 1.0.2-3 is now installed in C:\Program Files\Lua\5.1\systree (license: MIT/X license)
安装成功
使用vcpkg 安装
vcpkg install libffi --triplet x64-windows
choco install meson
meson .. -Dlua_version=vendor -Dlibffi=vendor -Dshared_libffi=true
拷贝文件到目录