手动编译lua模块lua-cjson

本文档详细介绍了如何在CentOS7环境下,针对32位Lua环境,编译lua-cjson的.so库文件。首先介绍了lua-cjson在OpenResty中的作用和特性,特别是关于空表格的编码选项。然后,通过luarocks安装lua-cjson的常规步骤,并展示了如何使用gcc编译32位的.so文件,包括所需的依赖和编译参数。最终,成功编译出32位的cjson.so并应用于项目中。
摘要由CSDN通过智能技术生成

环境

  • centos7

cjson

mpx/lua-cjson 是一个json编解码的module

openresty的fork 做了一些bug修复和增强

This fork of mpx/lua-cjson is included in the OpenResty bundle and includes a few bugfixes and improvements, especially to facilitate the encoding of empty tables as JSON Arrays.

lua里由于不能区分空对象还是空数组,使得mpx/lua-cjson 默认将{} encode成了 {},而不是想要的空数组[]

我这里要使用openresty的fork,并设置

encode_empty_table_as_object

syntax: cjson.encode_empty_table_as_object(true|false|"on"|"off")

Change the default behavior when encoding an empty Lua table.

By default, empty Lua tables are encoded as empty JSON Objects ({}). If this is set to false, empty Lua tables will be encoded as empty JSON Arrays instead ([]).

This method either accepts a boolean or a string ("on", "off").

这里主要分享下so文件的编译

64位的so文件

由于要在centos下使用,需要将源码编译成.so文件。luarocks可以帮助完成一些工作

找到luarocks lua-cjson的主页,前提是系统已安装luarocks,

# 确认已安装luarocks
[root@localhost ~]# luarocks --version
/usr/local/bin/luarocks 3.8.0
LuaRocks main command-line interface
# 通过luarocks 安装 lua-cjson
[root@localhost ~]# luarocks install  lua-cjson
Warning: falling back to wget - install luasec to get native HTTPS support
Installing https://luarocks.org/lua-cjson-2.1.0.6-1.src.rock

lua-cjson 2.1.0.6-1 depends on lua >= 5.1 (5.1-1 provided by VM)
gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c lua_cjson.c -o lua_cjson.o
gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c strbuf.c -o strbuf.o
gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c fpconv.c -o fpconv.o
gcc -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o
lua-cjson 2.1.0.6-1 is now installed in /usr/local (license: MIT)

根据上面命令行的执行结果发现已经编译好了cjson.so,不过该cjson.so是64位的,

一般在64位linux下(安装好ggc等工具后)编译出来的程序或者库都是针对64位环境的,如果要编译出32位的只需要在gcc的参数加上-m32

这里因为我的lua是32位的,需要32位的库,下面使用源码编译32位的。

源码编译32位cjson.so

首先clone下openresty的fork repo到本地

git clone https://github.com/openresty/lua-cjson

通过上面luarocks的执行步骤,我们大概知道是怎么编译的,照葫芦画瓢就行,不过我们这里要指定gcc编译参数为32位的

首先安装依赖的环境

yum install glibc-devel.i686
yum install libstdc++-devel.i686

大概解释下上面的编译参数,以这个命令为例

gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c lua_cjson.c -o lua_cjson.o

-O2表示优化级别,关于-fPIC的含义,sof这个链接有解释,-I后面跟的是目录,-c只complie和assemble不做link,-o是指定输出。最后看下32位和64位的编译参数

-m32
-m64
Generate code for 32-bit or 64-bit environments of Darwin and SVR4
targets (including GNU/Linux). The 32-bit environment sets int,
long and pointer to 32 bits and generates code that runs on any
PowerPC variant. The 64-bit environment sets int to 32 bits and
long and pointer to 64 bits, and generates code for PowerPC64, as
for -mpowerpc64.

这里都是从gcc的man页面整理出来的,直接搜就能找到

因此我们的编译命令就是,最后就得到了32位的cjson.so,cp到工程里就可以使用了

[root@localhost lua-cjson]# pwd
/tmp/test/lua-cjson
[root@localhost lua-cjson]# gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c /tmp/test/lua-cjson/lua_cjson.c -o lua_cjson.o
[root@localhost lua-cjson]# gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c /tmp/test/lua-cjson/strbuf.c -o strbuf.o
[root@localhost lua-cjson]# gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c /tmp/test/lua-cjson/fpconv.c -o fpconv.o
[root@localhost lua-cjson]# gcc -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o

have fun

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值