安装
http://luarocks.github.io/luarocks/releases/
配置
新建配置文件 C:\Users\Administrator\AppData\Roaming\luarocks\config-5.4.lua,CC和LD代表编译库源代码的编译器。
rocks_trees = { "E:/ScoopApps/apps/luarocks/current/rocks" }
lua_interpreter = "lua54.exe"
lua_version = "5.4"
variables = {
LUA_BINDIR = "E:/ScoopApps/apps/lua/current",
LUA_DIR = "E:/ScoopApps/apps/lua/current",
LUA_LIBDIR = 'E:/ScoopApps/apps/lua/current',
CC = "gcc",
LD = "gcc",
}
安装lua-cjson库
D:\>luarocks install lua-cjson
Installing https://luarocks.org/lua-cjson-2.1.0.10-1.src.rock
lua-cjson 2.1.0.10-1 depends on lua >= 5.1 (5.4-1 provided by VM)
gcc -O2 -c -o lua_cjson.o -IE:/ScoopApps/apps/lua/current/include lua_cjson.c -DDISABLE_INVALID_NUMBERS -DUSE_INTERNAL_ISINF
gcc -O2 -c -o strbuf.o -IE:/ScoopApps/apps/lua/current/include strbuf.c -DDISABLE_INVALID_NUMBERS -DUSE_INTERNAL_ISINF
strbuf.c: In function 'debug_stats':
strbuf.c:98:17: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
98 | (long)s, s->reallocs, s->length, s->size);
| ^
strbuf.c: In function 'strbuf_resize':
strbuf.c:172:17: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
172 | (long)s, s->size, newsize);
| ^
gcc -O2 -c -o fpconv.o -IE:/ScoopApps/apps/lua/current/include fpconv.c -DDISABLE_INVALID_NUMBERS -DUSE_INTERNAL_ISINF
gcc -shared -o cjson.dll lua_cjson.o strbuf.o fpconv.o E:/ScoopApps/apps/lua/current/lua54.dll -lm
lua-cjson 2.1.0.10-1 is now installed in E:/ScoopApps/apps/luarocks/current/rocks (license: MIT)
为了简化操作,将编译出来的cjson.dll复制到lua的安装目录。有点库还需要复制llua文件。
测试
local cjson = require "cjson"
local json = cjson.encode({
foo = "bar",
some_object = {},
some_array = cjson.empty_array
})
print(json)