luajit 安装cjson

2020-03-04 15:47:01 浏览数 (1)

最近需要升级原有服务器的nginx加载逻辑,新的lua脚本需要解析一个远程返回的json格式的结果,原有的luajit并没有带cjson库,需要自己手动安装一下。

基本参考博客luajit安装cjson。

cjson下载地址:http://www.kyne.com.au/~mark/software/lua-cjson.php

下载文件 lua-cjson-2.1.0.tar.gz

运行命令

代码语言:javascript复制
tar -zxvf lua-cjson-2.1.0.tar.gz 

cd  lua-cjson-2.1.0

make

make命令在使用默认配置下报错

代码语言:javascript复制
lua_cjson.c:43:17: error: lua.h: No such file or directory
lua_cjson.c:44:21: error: lauxlib.h: No such file or directory
lua_cjson.c:192: error: expected ‘)’ before ‘*’ token
lua_cjson.c:206: error: expected ‘)’ before ‘*’ token
lua_cjson.c:218: error: expected ‘)’ before ‘*’ token
lua_cjson.c:237: error: expected ‘)’ before ‘*’ token
lua_cjson.c:266: error: expected ‘)’ before ‘*’ token
lua_cjson.c:279: error: expected ‘)’ before ‘*’ token
lua_cjson.c:288: error: expected ‘)’ before ‘*’ token
lua_cjson.c:296: error: expected ‘)’ before ‘*’ token
lua_cjson.c:304: error: expected ‘)’ before ‘*’ token
lua_cjson.c:336: error: expected ‘)’ before ‘*’ token

根据上述博客和之前安装luasocket的经验,还是没有找到lua源码进行编译报的错。

find 一下lua.h这个文件,发现位于/opt/luajit-2.0.0/include/luajit-2.0路径下。

修改Makefile文件,修改default配置为

代码语言:javascript复制
##                          multi-threaded application. Requries _pthreads_.

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

主要是修改了LUA_INCLUDE_DIR用于安装cjson;修改了PREFIX变量用来改变编译结果文件输出的路径

保存修改,执行命令

代码语言:javascript复制
make && make install
cc -c -O3 -Wall -pedantic -DNDEBUG  -I/opt/luajit-2.0.0/include/luajit-2.0 -fpic -o lua_cjson.o lua_cjson.c
cc -c -O3 -Wall -pedantic -DNDEBUG  -I/opt/luajit-2.0.0/include/luajit-2.0 -fpic -o strbuf.o strbuf.c
cc -c -O3 -Wall -pedantic -DNDEBUG  -I/opt/luajit-2.0.0/include/luajit-2.0 -fpic -o fpconv.o fpconv.c
cc  -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o
mkdir -p //opt/luajit-2.0.0/lib/lua/5.1
cp cjson.so //opt/luajit-2.0.0/lib/lua/5.1
chmod 755 //opt/luajit-2.0.0/lib/lua/5.1/cjson.so

输出结果显示正确,安装成功

0 人点赞