2018-12-11 安装lua&luajit&cjson

安装lua5.3

sudo apt-get install libreadline-dev 
curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gz
tar zxf lua-5.3.0.tar.gz
cd lua-5.3.0
make linux test
make install

安装luajit

参考:
http://blog.gezhiqiang.com/2017/08/24/lua-cjson/

//下载
git clone http://luajit.org/git/luajit-2.0.git
tar zxf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
//编译
make
//安装
sudo make install

make 时报错

In file included from lj_ffrecord.c:858:0:
lj_recdef.h:224:1: error: ‘recff_rawlen’ undeclared here (not in a function)
 recff_rawlen,
 ^
Makefile:651: recipe for target 'lj_ffrecord.o' failed
make[1]: *** [lj_ffrecord.o] Error 1
make[1]: Leaving directory '/home/test/david/lua/luajit-2.0/src'
Makefile:109: recipe for target 'default' failed
make: *** [default] Error 2

参考:https://blog.csdn.net/sotower/article/details/46695877
执行

find . -type f -print0 | xargs -0 dos2unix

后重新make 正常。

root@linuxtest:/home/test/david/lua/luajit-2.0# luajit -v
LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/

安装cjson

修改makefile

##### Build defaults #####
LUA_VERSION =       5.1
TARGET =            cjson.so
PREFIX =            /usr/local
#CFLAGS =            -g -Wall -pedantic -fno-inline
CFLAGS =            -O3 -Wall -pedantic -DNDEBUG
CJSON_CFLAGS =      -fpic
CJSON_LDFLAGS =     -shared
LUA_INCLUDE_DIR =   $(PREFIX)/include/luajit-2.1
LUA_CMODULE_DIR =   $(PREFIX)/lib/lua/$(LUA_VERSION)
LUA_MODULE_DIR =    $(PREFIX)/share/lua/$(LUA_VERSION)
LUA_BIN_DIR =       $(PREFIX)/bin

cjson.so在:/usr/local/lib/lua/5.1下面默认有的(不知道是不是之前编译生成过的)

修改makefile后重新make,报错

lua_cjson.c:1312:13: error: static declaration of ‘luaL_setfuncs’ follows non-static declaration
 static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)
             ^
In file included from lua_cjson.c:44:0:
/usr/local/include/luajit-2.1/lauxlib.h:88:18: note: previous declaration of ‘luaL_setfuncs’ was here
 LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
                  ^
Makefile:90: recipe for target 'lua_cjson.o' failed
make: *** [lua_cjson.o] Error 1

参考:https://blog.csdn.net/kunlilove521/article/details/79992579
错误的大概意思是:luaL_setfuncs 这个方法 接口定义时不是静态的方法,实现时,却定义成了静态方法
解决方法:
直接在Makefile所在的目录执行查找字符串 命令:find . -type f -name "." | xargs grep "luaL_setfuncs" 结果如下
[root@MQ_96_86 lua-cjson-2.1.0]# find . -type f -name "." | xargs grep "luaL_setfuncs"
./lua_cjson.c: * luaL_setfuncs() is used to create a module table where the functions have
./lua_cjson.c:static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)
./lua_cjson.c: luaL_setfuncs(l, reg, 1);

发现只有lua_cjson.c 文件中包含上面字符串,所以编辑 lua_cjson.c
根据错误提示大概在88行,也可以vim打开文件后 查找字符串 luaL_setfuncs 定位到定义此方法的一行,将static关键字去掉,保存退出

找到

static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)

将static去掉重新make正常。

sudo make install

生成cjson.so

root@linuxtest:/home/test/david/lua/lua-cjson-master# sudo make install
mkdir -p //usr/local/lib/lua/5.1
cp cjson.so //usr/local/lib/lua/5.1
chmod 755 //usr/local/lib/lua/5.1/cjson.so

测试代码

local cjson = require "cjson"
-- local cjson_safe = require("cjson.safe")

local obj = {
    name = "david",
    age = 18
}
local json_str = cjson.encode(obj)
print(json_str)

因为系统中默认装的lua是5.2的(lua5.1是在luajit下的),导致直接lua json.lua报错。必须用luajit(使用的是lua5.1)执行。luajit路径是/usr/local/luajit

root@linuxtest:/home/test/david/lua# luajit json.lua
{"name":"david","age":18}

你可能感兴趣的:(2018-12-11 安装lua&luajit&cjson)