Lua笔记——1.Build Lua 开发环境

在Win、Linux及Mac上搭建Lua开发环境

Win

首先,在Lua官网下载Lua源码,然后,通过以下任意方法将下载的Lua源码编译成lua库文件,lua解释器,lua编译器

批处理编译

第一种方法:使用VS的Command Prompt命令行进行批处理编译

提前先阅读下官网说明

luawin.PNG

一、在源码的src目录同级下,新建一个luavs.bat的批处理文件

//file: luavs.bat
cd src
cl /O2 /W3 /c /DLUA_BUILD_AS_DLL l*.c
del lua.obj luac.obj
link /DLL /out:lua53.dll l*.obj
cl /O2 /W3 /c /DLUA_BUILD_AS_DLL lua.c luac.c
link /out:lua.exe lua.obj lua53.lib
del lua.obj
link /out:luac.exe l*.obj
cd ..

二、进入CommandPrompt执行编译

commandprompt.PNG

三、执行编译成功生成lua库文件、lua解释器、lua编译器

res.PNG

四、测试运行下生成的lua.exe

LuaRun.PNG

五、上述步骤完成后lua就可以正常使用了,之后可以选择进一步方便自己的使用,安装luarocks以及luafilesystem

luarocks

luarocks , the package manager for the Lua programming language.

LuaRocks allows you to install Lua modules as self-contained packages called rocks, which also contain dependency information. LuaRocks supports both local and remote repositories, and multiple local rocks trees.

luafilesystem

luafilesystem , the Lua library developed to complement the set of functions related to file systems.

LuaFileSystem offers a portable way to access the underlying directory structure and file attributes.

VS分别编译

第二种方法:使用VS分别编译生成lua53.lib静态库文件、lua.exe解释器、luac.exe编译器

vsbuild.PNG

一、lua53.lib

1.新建Visual C++空工程,命名为lua53,Source Files筛选项下添加现有项:Lua源码中src目录下除lua.c、luac.c和lua.hpp三个文件之外的全部文件

2.设置项目配置类型为静态库,不使用预编译头

3.Build

二、lua.exe

1.新建Visual C++空工程,命名为lua,Source Files筛选项下添加现有项:Lua源码中src目录下除luac.c文件之外的全部文件

2.设置项目配置类型为Application,不使用预编译头

3.Build

三、lua.exe

1.新建Visual C++空工程,命名为luac,Source Files筛选项下添加现有项:Lua源码中src目录下除lua.c文件之外的全部文件

2.设置项目配置类型为Application,不使用预编译头

3.Build

vsbuildDebug.PNG

四、善后工作

1.Build完成之后,在Debug目录会生成所需的lua53.lib静态库文件、lua.exe解释器、luac.exe编译器

2.为了之后在VS中方便使用,可以新建include、lib文件夹,添加lua.h luaconf.h lualib.h lauxlib.h lua.hpp五个文件到include文件夹中,添加lua53.lib到lib文件夹中

3.为了方便使用还可以设置lua.exe及luac.exe的系统环境变量

运行测试:

vsLua.PNG

五、方便lua的使用来安装luarocks以及luafilesystem

安装方法同方法一第五步

Lua for Windows

第三种方法,可以直接使用Lua for Windows(只不过这个Lua的版本好久没更新了,但是环境、luarocks、luafilesystem都是设置好了的,不用自己配置,推荐使用),或者 Lua官方推荐的方法 LuaDist

直接安装然后添加至环境变量,即可使用

LuaForWin.PNG

目录文件:

Lua51.PNG

安装完成之后,可以很方便的为VS工程添加"可执行文件目录"、"包含目录"、"库目录"的路径信息

VC++Directories.PNG

将"附加依赖项"中添加上"lua51.lib;lua5.1.lib";

linker.PNG

测试Lua环境C代码(Win + VS2017 , Lua 5.3):

//file: cCodeTest.c
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    if (luaL_dofile(L, "luaCode.lua"))
        printf("Failed to load and run the lua code .");

    lua_close(L);
    return 0;
}

当前工作目录下luaCode.lua代码:

--file: luaCode.lua
print("Executed From C .
Hello Lua ! ")
print(_VERSION)

输出结果:

luaVS.PNG

测试Lua环境C++代码(Win + VS2017 , Lua 5.3):

//file: cCodeTest.cpp
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}

#include <iostream>
using namespace std;

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    if (luaL_dofile(L, "luaCode.lua"))
        cout << "Failed to load and run the lua code ." << endl;

    lua_close(L);
    char c;
    cin >> c;
    return 0;
}

也可直接
#include <lua.hpp>
完整代码:

//file: cCodeTest.cpp
#include <lua.hpp>
#include <iostream>
using namespace std;

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    if (luaL_dofile(L, "luaCode.lua"))
        cout << "Failed to load and run the lua code ." << endl;

    lua_close(L);
    char c;
    cin >> c;
    return 0;
}

运行结果同上

Linux

Linux(Ubuntu为例)下 Build

确保make、build-essential、libreadline以及libreadline-dev等包已安装(可以使用apt-file search readline | grep readline.h等命令来查找缺失的包,之后使用sudo apt-get update ; sudo apt-get install make等命令来安装)

安装完必要的软件包后,就可以直接sudo apt-get install lua5.3自动安装;

也可以选择在lua官上下载源码到Linux上自己编译

# Where to install. The installation starts in the src and doc directories,
# so take care if INSTALL_TOP is not an absolute path. See the local target.
# You may want to make INSTALL_LMOD and INSTALL_CMOD consistent with
# LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h.
INSTALL_TOP= /usr/local
INSTALL_BIN= $(INSTALL_TOP)/bin
INSTALL_INC= $(INSTALL_TOP)/include
INSTALL_LIB= $(INSTALL_TOP)/lib
INSTALL_MAN= $(INSTALL_TOP)/man/man1
INSTALL_LMOD= $(INSTALL_TOP)/share/lua/$V
INSTALL_CMOD= $(INSTALL_TOP)/lib/lua/$V

# What to install.
TO_BIN= lua luac
TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp
TO_LIB= liblua.a
TO_MAN= lua.1 luac.1

可以根据自己需要修改Makefile文件中的安装路径或者安装之后自己添加环境变量

curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz
tar zxf lua-5.3.5.tar.gz
cd lua-5.3.5
make linux test
sudo make install

安装完成后就可以运行Lua了

LinuxLua.PNG

luarocks

luarocks , the package manager for the Lua programming language.

LuaRocks allows you to install Lua modules as self-contained packages called rocks, which also contain dependency information. LuaRocks supports both local and remote repositories, and multiple local rocks trees.

lua和luarocks都按默认配置编译安装,在luarocks目录下使用./configure,会出现配置完成的信息

LinuxRockCon.PNG

然后运行:

make build

sudo run make install

安装完成之后,可以很容易的管理Lua包

luarocks install dkjson

luarocks show dkjson

LinuxDKJson.PNG

--file: testJson.lua

local jsonUtil = require "dkjson"

print(jsonUtil.version)

使用第三方lua包成功

testJson.PNG

luafilesystem

luafilesystem , the Lua library developed to complement the set of functions related to file systems.

LuaFileSystem offers a portable way to access the underlying directory structure and file attributes.

Linux 上在安装了luarocks之后,可以使用命令安装

luarocks install luafilesystem

Linuxlfs.PNG

测试C++代码:

//file: cCodeTest.cpp
#include <lua.hpp>
#include <iostream>
using namespace std;

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    if (luaL_dofile(L, "luaCode.lua"))
        cout << "Failed to load and run the lua code ." << endl;

    lua_close(L);
    char c;
    cin >> c;
    return 0;
}

Lua代码:

--file: luaCode.lua

print("Executed From C .
Hello Lua ! ")
print(_VERSION)

编译运行:

g++ -o test cCodeTest.cpp -llua -lm -ldl
./test

运行结果:

linuxluac.PNG

Mac

Building&Install

同Linux一样进行即可


curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz
tar zxf lua-5.3.5.tar.gz
cd lua-5.3.5
make macosx test
sudo make install

安装结果:

MacLua.png

REF

文档:

http://www.lua.org/download.html

http://lua-users.org/wiki/BuildingLua

https://github.com/luarocks/luarocks/wiki

博客:
http://www.hahack.com/codes/cmake/

https://blog.csdn.net/uisoul/article/details/60137134?locationNum=10&fps=1

https://www.linuxidc.com/Linux/2014-02/96459.htm

原文地址:https://www.cnblogs.com/sylvan/p/8478346.html