手动编译lua模块lua-cjson

手动编译lua模块lua-cjson,第1张

环境
  • 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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/791994.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-05
下一篇2022-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存