arm-cortex A9调用lua 解释器

   日期:2020-06-01     浏览:103    评论:0    
核心提示:1.编译lua-3.3.5下载lua源文件:curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz修改 src/MakefilePLAT= none改为:PLAT= linuxCC= gcc -std=gnu99改为:CC= /opt/Xilinx/SDK/2018.3/gnu/aarch32/lin/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-gcc (根据实际交叉编译器位置,作相.lua

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();
    }
    
    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 文件夹拷贝到开发板上

运行如下:

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
更多>相关资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服