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

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

Win

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

批处理编译

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

前提先阅读下官网说明

CO0kEDo.png

一、在源码的src目录同级下,新建一个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执行编译

hWMIUrG.png

三、编译生成lua的库文件、lua解释器、lua编译器

C5EfOV2.png

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

ZNKKTA5.png

五、开始安装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编译器

EoZB6Xs.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

YRVwwVJ.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的系统环境变量

运行情况:

UoeIZzx.png

五、开始安装luarocks以及luafilesystem

安装方法同方法一第五步

Lua for Windows

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

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

LIMhgSt.png

安装目录:

fCbbuE9.png

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

wgy75L6.png

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

YaZu22D.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 .\nHello Lua ! ")
    print(_VERSION)

输出结果:

jbBJEGS.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等命令来安装)


    # 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了

SY7qkuO.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,会出现配置完成的信息

Fh6gYy5.png

然后运行:

make build

sudo run make install

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


    luarocks install dkjson
    
    luarocks show dkjson

vSCSObn.png


    --file: testJson.lua
    
    local jsonUtil = require "dkjson"
    
    print(jsonUtil.version)

使用第三方lua包成功

eFq99pC.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

oJpOJbm.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 .\nHello Lua ! ")
    print(_VERSION)

编译运行:


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

运行结果:

yZEe9bT.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

安装结果:

5c8c65f76e8d1.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值