1.編譯lua-3.3.5
下載lua源文件:
curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz
修改 src/Makefile
PLAT= none
改爲:
PLAT= linux
CC= gcc -std=gnu99
改爲:
CC= /opt/Xilinx/SDK/2018.3/gnu/aarch32/lin/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-gcc (根據實際交叉編譯器位置,作相應更改)
AR= ar rcu
改爲:
AR= /opt/Xilinx/SDK/2018.3/gnu/aarch32/lin/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-ar rcu
RANLIB= ranlib
改爲:
RANLIB= /opt/Xilinx/SDK/2018.3/gnu/aarch32/lin/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-ranlib
linux:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl -lreadline"
改爲:
linux:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl"
編譯源碼:
cd lua-5.3.5/src
make
在src目錄會生成
liblua.a:lua靜態鏈接庫
lua程序:lua解釋器
luac程序:luac是將lua的程序文件編譯成二進制文件的工具
2.使用lua靜態庫
配置QT交叉編譯環境後,新建工程:lua_qttest.
.pro文件如下:
QT += core
QT -= gui
CONFIG += c++11
TARGET = lua_qttest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
unix:!macx: LIBS += -L$$PWD/lib/ -llua
INCLUDEPATH += $$PWD/include
DEPENDPATH += $$PWD/include
HEADERS += \
include/lua.hpp
LIBS += -ldl
DESTDIR += $$PWD/bin
#unix:!macx: PRE_TARGETDEPS += $$PWD/lib/liblua.a
qt編譯會出現 [undefined reference to symbol ‘dlsym@@GLIBC_2.4’],在.pro中其中添加LIBS += -ldl
編寫程序
main.cpp文件:
#include <QCoreApplication>
#include <iostream>
#include <stdio.h>
#include <lua.hpp>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("I am QT5.7\n");
lua_State *L;
L = luaL_newstate();
if(L == NULL){
printf("luaL_newstate failed\n");
return a.exec();
}
/*load Lua base libraries*/
luaL_openlibs(L);
if(luaL_dofile(L,"bin/cal.lua")){
const char *error = lua_tostring(L, -1);
std::cout << error <<std::endl;
return false;
}
lua_close(L);
printf("luaL_newstate OK\n");
return a.exec();
}
cal.lua:打印0-100之間的偶數
print("print even numbers between 0 to 100\n")
for i = 0,100,1 do
if i%2 == 0 then
print(i,"\n")
end
end
3.開發板運行
將 bin include lib lua_qttest 文件夾拷貝到開發板上
運行如下: