arm-cortex A9调用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();
    }
    /*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 文件夹拷贝到开发板上

运行如下:
arm-cortex A9调用lua 解释器

上一篇:破解wifi密码


下一篇:创龙TI AM437x ARM Cortex-A9 CPU开发板规格书