安装cjson的前提的已经安装好了LuaJIT-2.0.4
# wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
1、安装cjson
#wget -O lua-cjson-2.1.0.tar.gz $download_Url/install/src/lua-cjson-2.1.0.tar.gz -T 20
#wget -O https://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
wget http://192.168.124.242:8080/sxsafe/lua-cjson-2.1.0.tar.gz
tar xvf lua-cjson-2.1.0.tar.gz
rm -f lua-cjson-2.1.0.tar.gz
cd lua-cjson-2.1.0
make clean
make -B
make install
cd ..
rm -rf lua-cjson-2.1.0
ln -sf /usr/local/lib/lua/5.1/cjson.so /usr/lib64/lua/5.1/cjson.so
ln -sf /usr/local/lib/lua/5.1/cjson.so /usr/lib/lua/5.1/cjson.so
执行 make -B时报错:
root@VM-domU:/root/lua-cjson-2.1.0# make -B
cc -c -O3 -Wall -pedantic -DNDEBUG -I/usr/local/include -fpic -o lua_cjson.o lua_cjson.c
lua_cjson.c:43:10: fatal error: lua.h: 没有那个文件或目录
43 | #include <lua.h>
| ^~~~~~~
compilation terminated.
make: *** [Makefile:89:lua_cjson.o] 错误 1
处理方式:
参考:https://www.cnblogs.com/liaojiafa/archive/2004/01/13/9111806.html
# 手动执行下面4命令
cc -c -O3 -Wall -pedantic -DNDEBUG -I/usr/local/include/luajit-2.0/ -fpic -o lua_cjson.o lua_cjson.c
cc -c -O3 -Wall -pedantic -DNDEBUG -I/usr/local/include -fpic -o strbuf.o strbuf.c
cc -c -O3 -Wall -pedantic -DNDEBUG -I/usr/local/include -fpic -o fpconv.o fpconv.c
cc -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o
# 如第一条报错,忽略
执行 make install 完成安装
# make install 完成安装cjson
root@VM-domU:/sxws/lua-cjson-2.1.0# make install
cc -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o
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
验证:
# vim /tmp/test.lua 写入如下内容:
local json = require "cjson"
local t = {name="joe", age=24}
local s = json.encode(t)
print(s)
执行 :lua /tmp/test.lua
root@HVM-domU:/tmp# lua /tmp/test.lua
{"name":"joe","age":24}