LUA Static linking

Just starting out? Need help? Post your questions and find answers here.
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

LUA Static linking

Post 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?
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: LUA Static linking

Post 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.
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: LUA Static linking

Post 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
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: LUA Static linking

Post 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
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: LUA Static linking

Post 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?
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: LUA Static linking

Post 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
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: LUA Static linking

Post 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
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: LUA Static linking

Post 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.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: LUA Static linking

Post by idle »

glad you got it sorted and thanks for the info.
tester
User
User
Posts: 30
Joined: Sun Dec 28, 2014 1:12 pm

Re: LUA Static linking

Post 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
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: LUA Static linking

Post 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()))
tester
User
User
Posts: 30
Joined: Sun Dec 28, 2014 1:12 pm

Re: LUA Static linking

Post by tester »

The standalone exe file is ~180-190 KB.
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: LUA Static linking

Post by Justin »

Great i'll give it a try, that's much easier, thanks.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: LUA Static linking

Post by jack »

in regards to the __imp___acrt_iob_func linker error the solution is to link against libmsvcrt-os
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: LUA Static linking

Post 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
Post Reply