Page 1 of 2

LUA Static linking

Posted: Sat Jul 17, 2021 8:30 pm
by Justin
Has anyone tried to statically link LUA scripting language?
https://www.lua.org/home.html

Building the sources is relatively simple, you can download them here:
https://www.lua.org/ftp/lua-5.4.3.tar.gz

Then i followed this video tutorial:
https://www.youtube.com/watch?v=TALXtup2CjI
You only have to change from Dynamic Library to Static Library when you reach that point and compiles fine with Visual Studio 2019.

I ended with a LUA.lib, but when imported from PB:

Code: Select all

Import "LUA.lib"
luaL_newstate()
EndImport
I got "unable to find symbol 'fprintf' in member x64\Release\liolib.obj", after some research a added the dependencies (in VS project settings Librarian):
legacy_stdio_definitions.lib
legacy_stdio_wide_specifiers.lib
ucrt.lib

And the lib is imported but every time i use a function like luaL_newstate() i get:
POLINK fatal error corrupt library unable to find symbol luaL_newstate in member lauxlib.obj

There are some old posts about this but none really work, using the mrthod from here by jack
http://forums.purebasic.com/english/vie ... 6&start=15
The lib compiles but shows polink errors just after importing.

Any hints?

Re: LUA Static linking

Posted: Sat Jul 17, 2021 10:29 pm
by idle
this happens when you try to use a static lib that's built with a different version of VS than purebasic
you will need to add the microsoft crt libs as dependencies to the static lib.

Re: LUA Static linking

Posted: Sat Jul 17, 2021 11:19 pm
by Justin
hi idle,
I added
legacy_stdio_definitions.lib
legacy_stdio_wide_specifiers.lib
ucrt.lib

as dependencies when building the lib, is that what you are refering to?

also using this has the same effect that adding the dependencies, removing the first error:

Code: Select all

ImportC "msvcrt.lib"
  sprintf
  sscanf
 _snprintf
  fprintf
EndImport 
but what bugs me is that does not find any lua imported function

Re: LUA Static linking

Posted: Sun Jul 18, 2021 12:20 am
by idle
you need to add the static crt libs as dependencies you might not need all off them and it will result in a big library but it won't copy it all into your executable.
https://docs.microsoft.com/en-us/cpp/c- ... w=msvc-160
libucrt.lib
libvcruntime.lib
libcmt.lib
libcpmt.lib

Re: LUA Static linking

Posted: Sun Jul 18, 2021 9:48 am
by Justin
Linking:
libucrt.lib
libvcruntime.lib
libcmt.lib
libcpmt.lib

Produces polink error unable to find sprintf, wich can be removed either linking:
legacy_stdio_definitions.lib
legacy_stdio_wide_specifiers.lib

or
ImportC "msvcrt.lib"
sprintf
; sscanf
; _snprintf
; fprintf
EndImport

but in turn makes the debugger quit unexpectly.

Maybe compiling with the same vs version pb used? Do you know wich one is?

Re: LUA Static linking

Posted: Sun Jul 18, 2021 4:24 pm
by Justin
I compiled with mingw instead of vs and after doing this:

Code: Select all

Import "libgcc.a"
EndImport

Import "libmingw32.a"
EndImport

Import "libmingwex.a"
EndImport

Import "liblua.a"
	luaL_newstate()
EndImport

Debug luaL_newstate()
i only get 2 unresolved externals:
__imp__acrt_iob_func from liblua.a
_get_output_format from libmingwex.a

Re: LUA Static linking

Posted: Sun Jul 18, 2021 7:39 pm
by Justin
I managed to get rid of the _get_output_format error creating a static lib with that function that returns 0
https://docs.microsoft.com/en-us/cpp/c- ... w=msvc-160

Fixing the __imp__acrt_iob_func problem seems more complicated, there is a discussion here:
https://stackoverflow.com/questions/304 ... -func-sdl2

https://www.freebasic.net/forum/viewtopic.php?t=26422

Re: LUA Static linking

Posted: Sun Jul 18, 2021 11:25 pm
by Justin
It finally worked, after creating a static lib implementing __imp___acrt_iob_func as suggested in the links i posted, so the helper static lib now contains both functions.

so the code:

Code: Select all

MessageRequester("LUA", Str(luaL_newstate()))
Produces a standalone 180kb exe with LUA embebed with no dlls.

BUT, it only works with PB 6 Alpha, probably due to the new gcc stuff or new libraries.
With PB 5.73 i get a blank POLINK error with no message.
I am not sure what will happen when __imp___acrt_iob_func is called, i will test the lib tomorrow.
I think it should be an easier way with older VS version but i only have 2019.

Re: LUA Static linking

Posted: Sun Jul 18, 2021 11:39 pm
by idle
glad you got it sorted and thanks for the info.

Re: LUA Static linking

Posted: Mon Jul 19, 2021 2:18 pm
by tester
The static library linked successfully with PB 5.73 - 6 Alpha x86/x64 when compiled in VS2013 with the following settings:

Code: Select all

General -> Whole Program Optimization:NO
Librarian -> Additional Dependencies:msvcrt.lib

Re: LUA Static linking

Posted: Mon Jul 19, 2021 6:24 pm
by Justin
I suspected it should work, i'll give it a try when i get VS2013.
Just curious, how big is the executable with this code?

Code: Select all

MessageRequester("LUA", Str(luaL_newstate()))

Re: LUA Static linking

Posted: Mon Jul 19, 2021 7:05 pm
by tester
The standalone exe file is ~180-190 KB.

Re: LUA Static linking

Posted: Mon Jul 19, 2021 7:17 pm
by Justin
Great i'll give it a try, that's much easier, thanks.

Re: LUA Static linking

Posted: Mon Jul 19, 2021 9:01 pm
by jack
in regards to the __imp___acrt_iob_func linker error the solution is to link against libmsvcrt-os

Re: LUA Static linking

Posted: Mon Jul 19, 2021 10:50 pm
by Justin
Hi jack thanks for helping out,

do you mean when linking with mingw gcc? how?
i just tried to import it from pb and i got error entry point not found in PostThreadMessageA ...

Code: Select all

Import "libmsvcrt-os.a"
EndImport
like tester said compiling with VS2013 worked fine