Hi, i'm trying to integrate Lua. Seems like it should work with no problem but i have some strange linking error. I tried different version of the GCC toolchain as it seems that newest one will give even more errors. I'm using gcc-arm-none-eabi-4_8-2014q3, which should match the one used by fpc (correct me if i'm wrong).
First i downloaded lua sources and compile them with the following commands (adapted from the makefile):
Code: Select all
arm-none-eabi-gcc -mabi=aapcs -march=armv6 -marm -mfpu=vfp -mfloat-abi=hard -D__DYNAMIC_REENT__ -DLUA_32BITS -O2 -c *.c
arm-none-eabi-ar rcs liblua.a lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o lmathlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o loadlib.o linit.o
Now i have a liblua.a. I copied into the project folder, then wrote the following simple "header file" (ctypes is not really needed but i was trying everything).
Code: Select all
unit LUAUnit;
{$linklib lua}
{$mode objfpc}{$H+}
interface
uses
Syscalls,ctypes;
const
LUA_OK : Integer = 0;
LUA_MULTRET: Integer = -1;
type Plua_State = pointer;
function luaL_newstate(): Plua_State; cdecl; external 'lua' name 'luaL_newstate';
procedure luaL_openlibs(luaState: Plua_State); cdecl; external 'lua' name 'luaL_openlibs';
function luaL_loadfile(luaState: Plua_State; filename: PChar): Integer; cdecl; external 'lua' name 'luaL_loadfile';
function lua_pcall(luaState: Plua_State; nargs, nresults, errfunc: Integer): Integer; cdecl; external 'lua' name 'lua_pcall';
procedure lua_call(luaState: Plua_State; nargs:cint32; nresults: cint32); cdecl; external 'lua' name 'lua_call';
function luaL_loadstring(luaState: Plua_State; s: PChar): Integer; cdecl; external 'lua' name 'luaL_loadstring';
implementation
end.
It's just a start to see if i can execute any Lua code at all. This is the test procedure:
Code: Select all
procedure runLua;
var luas:Plua_State;
res:integer;
begin
luas := luaL_newstate();
ConsoleWindowWriteLn(WindowHandle,'LUA handle is: '+inttostr(qword(luas)));
luaL_openlibs(luas);
res:=luaL_loadstring(luas, 'print("hello from lua");print(_VERSION)');
if res <> LUA_OK then raise exception.create('NOT loaded');
ConsoleWindowWriteLn(WindowHandle,'Loaded : '+IntToStr(res));
//lua_pcall(luas, 0, LUA_MULTRET,0);
if res <> LUA_OK then raise exception.create('NOT executed');
end;
Now as it is, it works. I can see that luaL_newstate returns a valid pointer and luaL_loadstring returns LUA_OK (0). The problem is, if i uncomment the lua_pcall line to actually execute the code, the program will break at linking with the following errors:
Code: Select all
Free Pascal Compiler version 3.1.1 [2017/09/18] for arm
Copyright (c) 1993-2015 by Florian Klaempfl and others
(1002) Target OS: Ultibo
(3104) Compiling HelloGLES.lpr
C:\my\path\GlesRPz\HelloGLES.lpr(46,64) Warning: (4056) Conversion between ordinals and pointers is not portable
C:\my\path\GlesRPz\HelloGLES.lpr(24,1) Hint: (5023) Unit "RaspberryPi" not used in HelloGLES
(9009) Assembling hellogles
(9015) Linking HelloGLES
C:\my\path\GlesRPz\lib\arm-ultibo\HelloGLES.o: In function `P$HELLOGLES_$$_RUNLUA':
HelloGLES.lpr:(.text.n_p$hellogles_$$_runlua+0x110): undefined reference to `lua_pcall'
C:\my\path\GlesRPz\HelloGLES.lpr(93,58) Error: (9013) Error while linking
C:\my\path\GlesRPz\HelloGLES.lpr(93,58) Fatal: (10026) There were 1 errors compiling module, stopping
Fatal: (1018) Compilation aborted
Error: C:\Ultibo\Core\fpc\3.1.1\bin\i386-win32\ppcrossarm.exe returned an error exitcode
It's very strange that it can't see lua_pcall, but can see the other two functions. Doing some tests, i see that some functions are ok, while others no. luaL_loadfile is another that can't be found.
But I'm sure all of them are inside my liblua.a (you can find it here)
Any idea of what could be going on? Why some functions are visible and others aren't?
Thanks
