如何在树莓派4B的Lua 5.3环境下正确加载lua-periphery库?
问题解决:Lua5.3下lua-periphery安装适配错误及运行报错
错误原因
你遇到的undefined symbol: lua_tointeger错误,是因为安装的lua-periphery是针对Lua5.1编译的——Lua5.1中的lua_tointeger函数在Lua5.3中已被废弃,替换为lua_tointegerx,导致5.3版本的Lua无法加载5.1版本的编译库。
解决步骤
1. 修正luarocks与Lua5.3的关联配置
你的luarocks2.3.0版本较老,默认可能关联的是Lua5.1,需要手动指定关联Lua5.3:
- 检查当前luarocks关联的Lua版本:
luarocks config lua_version - 如果输出为
5.1,执行以下命令重新配置:
(Raspbian中Lua5.3的头文件默认在sudo luarocks config lua_interpreter lua5.3 sudo luarocks config lua_dir /usr sudo luarocks config lua_version 5.3/usr/include/lua5.3,库文件在/usr/lib/arm-linux-gnueabihf,上述配置可正确指向)
2. 卸载旧版本lua-periphery
移除之前安装的Lua5.1适配版本:
sudo luarocks remove --tree luarocks lua-periphery
3. 重新安装lua-periphery
用修正后的luarocks配置重新安装,确保编译时使用Lua5.3的API:
sudo luarocks install --tree luarocks lua-periphery
安装完成后,检查luarocks/lib/lua/目录下会出现5.3文件夹,其中包含适配的periphery.so。
4. 修改脚本的路径配置
将脚本中指向Lua5.1的路径改为5.3:
package.path = 'luarocks/share/lua/5.3/?.lua;luarocks/share/lua/5.3/?/init.lua;' .. package.path package.cpath = 'luarocks/lib/lua/5.3/?.so;' .. package.cpath local gpio = require("periphery").GPIO buttons = {22, 23, 24, 26} for i=1, #buttons do local pin = buttons[i] buttons[i] = gpio("btt"..pin, pin, "in") end while true do os.execute("sleep 0.1") for i=1, #buttons do if buttons[i]:read() == true then print(i) end end end
5. 备选方案:手动编译lua-periphery(若luarocks自动安装仍失败)
如果luarocks2.3.0对Lua5.3的支持仍有问题,可手动编译源码:
- 下载lua-periphery源码包并解压
- 进入源码目录,执行编译命令(指定Lua5.3路径):
make LUA_INCDIR=/usr/include/lua5.3 LUA_LIBDIR=/usr/lib/arm-linux-gnueabihf LUA_LIB=lua5.3 - 安装到本地tree目录:
make install PREFIX=./luarocks
内容的提问来源于stack exchange,提问作者Yoaquin
相关产品推荐
相关产品推荐

