前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Openresty如何使用lualocks包管理器安装使用Lua包

Openresty如何使用lualocks包管理器安装使用Lua包

作者头像
Tinywan
发布2024-03-20 21:14:36
1550
发布2024-03-20 21:14:36
举报
文章被收录于专栏:开源技术小栈开源技术小栈

Luarocks是一个Lua包管理器,基于Lua语言开发,提供一个命令行的方式来管理Lua包依赖、安装第三方Lua包等,社区比较流行的包管理器之一,另还有一个LuaDist,Luarocks的包数量比LuaDist多。

更细节的两者对比可参阅这里 http://notebook.kulchenko.com/zerobrane/lua-package-managers-luadist-luarocks-and-integration-with-zerobrane-studio

在做一些openresty的项目的时候,经常会借助一些第三方包来协助开发,为了方便管理,我们可以使用openresy官方的opm,或者lua的包管理工具luarocks,只不过opm的包数量还不是太多,用的较多的还是luarocks,现在只能期待opm社区不断的发展壮大了。

编译安装

代码语言:javascript
复制
wget https://github.com/luarocks/luarocks/archive/v3.0.0.tar.gz
 
tar zxvf v3.0.0.tar.gz
 
cd luarocks-3.0.0/
 
./configure --prefix=/usr/local/openresty/luajit \
--with-lua=/usr/local/openresty/luajit/ \
--lua-suffix=jit \
--with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
 
make build
# 安装需要root权限
sudo make install

安装参数说明

  • --prefix 设定 luarocks 的安装目录
  • --with-lua 则是系统中安装的 lua 的根目录
  • --lua-suffix 版本后缀,此处因为openresyt的lua解释器使用的是 luajit ,所以此处得写 jit
  • --with-lua-include 设置 lua 引入一些头文件头文件的目录

查看版本

代码语言:javascript
复制
luarocks --version
/usr/local/openresty/luajit/bin/luarocks 3.0.0
LuaRocks main command-line interface 

提示错误

代码语言:javascript
复制
Configuring LuaRocks...
 
Lua version detected: 5.1
Lua interpreter found: /usr/local/openresty/luajit/bin/luajit
lua.h found: /usr/local/openresty/luajit/include/luajit-2.1/lua.h
Could not find 'unzip'.
Make sure it is installed and available in your PATH.
 
configure failed.

解决

代码语言:javascript
复制
sudo apt install unzip

安装Lua包

执行 luarocks install package 就可以安装lua的包了。luarocks install package --tree=path 还可以指定你安装的包的存放路径。

安装rapidjson

rapidjson 是有名的开源c++ json库,其类java的API使得其易于使用,然而对于rapidjson中的setInt,setString等等setXXX的函数,以及getInt, getString等等getXXX的函数,作者觉得太过于繁琐,想到如果能将这些set和get封装起来,仅仅使用一个函数接口来调用,那么代码将会显得很简洁,维护起来也容易多了。

安装

代码语言:javascript
复制
$ luarocks install rapidjson --tree=/usr/local/openresty/lualib/resty
Installing https://luarocks.org/rapidjson-0.5.1-1.src.rock
 
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- LUA_RAPIDJSON_VERSION: 0.5.1
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/luarocks_rapidjson-0.5.1-1-fd1Qr3/lua-rapidjson/build.luarocks
Scanning dependencies of target lua-rapidjson
[ 20%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/Document.cpp.o
[ 40%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/Schema.cpp.o
[ 60%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/rapidjson.cpp.o
[ 80%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/values.cpp.o
[100%] Linking CXX shared module rapidjson.so
[100%] Built target lua-rapidjson
[100%] Built target lua-rapidjson
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/openresty/lualib/resty/lib/luarocks/rocks-5.1/rapidjson/0.5.1-1/lib/rapidjson.so
rapidjson 0.5.1-1 is now installed in /usr/local/openresty/lualib/resty (license: MIT)

安装路径

代码语言:javascript
复制
$/usr/local/openresty/lualib/resty/lib/luarocks/rocks-5.1
ls
30log  luasocket  manifest  rapidjson

使用

代码语言:javascript
复制
local rapidjson = require('rapidjson')
 
rapidjson.encode()
rapidjson.decode()
 
rapidjson.load()
rapidjson.dump()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-03-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 开源技术小栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 编译安装
  • 查看版本
  • 安装Lua包
    • 安装rapidjson包
      • 安装
        • 安装路径
          • 使用
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档