luajit 安装cjson

时间:2023-12-22 14:21:44

最近需要升级原有服务器的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

运行命令

tar -zxvf lua-cjson-2.1..tar.gz 

cd  lua-cjson-2.1.0

make

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

lua_cjson.c::: error: lua.h: No such file or directory
lua_cjson.c::: error: lauxlib.h: No such file or directory
lua_cjson.c:: error: expected ‘)’ before ‘*’ token
lua_cjson.c:: error: expected ‘)’ before ‘*’ token
lua_cjson.c:: error: expected ‘)’ before ‘*’ token
lua_cjson.c:: error: expected ‘)’ before ‘*’ token
lua_cjson.c:: error: expected ‘)’ before ‘*’ token
lua_cjson.c:: error: expected ‘)’ before ‘*’ token
lua_cjson.c:: error: expected ‘)’ before ‘*’ token
lua_cjson.c:: error: expected ‘)’ before ‘*’ token
lua_cjson.c:: error: expected ‘)’ before ‘*’ token
lua_cjson.c:: error: expected ‘)’ before ‘*’ token

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

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

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

##                          multi-threaded application. Requries _pthreads_.

##### Build defaults #####
LUA_VERSION = 5.1
TARGET = cjson.so
PREFIX = /opt/luajit-2.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./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变量用来改变编译结果文件输出的路径

保存修改,执行命令

make && make install
cc -c -O3 -Wall -pedantic -DNDEBUG -I/opt/luajit-2.0./include/luajit-2.0 -fpic -o lua_cjson.o lua_cjson.c
cc -c -O3 -Wall -pedantic -DNDEBUG -I/opt/luajit-2.0./include/luajit-2.0 -fpic -o strbuf.o strbuf.c
cc -c -O3 -Wall -pedantic -DNDEBUG -I/opt/luajit-2.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 //opt/luajit-2.0.0/lib/lua/5.1/cjson.so

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