Correct files and location to place sgared libraries

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Post Reply
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Correct files and location to place sgared libraries

Post by Axle »

Hi FB team.

I know I have asked the question in one way or another previously, but can't seem to get a definitive answer on this. FB ships with many includes by default libname.bi. but we still need to import the binaries for some. FB 1.09.0 64bit Windows.

For simplicity just stay with shared libraries only. I will use curses.bi (pdcurses.bi) as an example.

Code: Select all

.\FreeBASIC-1.09.0-win64\inc\curses.bi
.\FreeBASIC-1.09.0-win64\inc\curses\pdcurses.bi
.\FreeBASIC-1.09.0-win64\lib\win64\ (No libpdcurse.dll.a present)
.\FreeBASIC-1.09.0-win64\bin\win64\ (No pdcurse.dll present)

Code: Select all

.\FB\curses\hellowordl.bas (Project directory)
Now this requires version 3.4 so I have from the pdcurse34 build ( mingwin32.mak WIDE=Y UTF8=Y DLL=Y):

Code: Select all

pdcurses.a
pdcurses.def
pdcurses.dll
1. Which of the 3 above files are typically needed to link against as a shared library?
2. What directory should each files/s be placed in?


==============================================================================
It would seam as if I only need the DLL and the lib.dll.a is not actually required (Unsure) and compiles and links against the pdcurses.dll in the project directory without the need for the libpdcuses.dll.a..
if so, why all of the libname.dll.a files in .\lib ?
Or is it normal have both .\lib\libpdcusres.dll.a as well as .\project\pdcurse.dll
This is not stated anywhere that I can find in the manuals, and haven't yet found it in the forums.

Axle
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Correct files and location to place sgared libraries

Post by caseih »

If you tell the compiler to link against the .a file, the result is a static binary that does not need the dll. The library is compiled in.

.Def files are not required by FBC, but it will use them if provided. The .def file provides the linker with information about the symbols in the dll. The dll is still required by the executable at run time, but the compiler does not need to see the dll when the def file is present.

Finally you can tell fbc to link directly against a dll and it will work just fine.

During compile, it does not particularly matter where the .a, .def, or .dll file is, provided you've passed the path to fbc using the -p command-line option so the compiler can find it. Or place them in the same directory as your source code file.

Generally-speaking, dlls should be in the same directory as the compiled executable when you go to run the exe. Technically I should say the dll should be in the current working directory of the binary. I believe Windows has a search path it uses if the required dll is not in the current working directory. But with modern windows, you really should be keeping your dependent dlls with your exe.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Correct files and location to place sgared libraries

Post by caseih »

Guess I should answer your questions directly.
Axle wrote: Nov 02, 2022 8:521. Which of the 3 above files are typically needed to link against as a shared library?
Either the def file or the dll file is needed at compile time. However at run time, only the dll is required.
2. What directory should each files/s be placed in?
It does not matter during compile time, provided you've given fbc a path to search with the -p argument. If using the def or dll, at run time you need to have the DLL with the exe. If you used the .a file, nothing is needed other than the exe.
It would seam as if I only need the DLL and the lib.dll.a is not actually required (Unsure) and compiles and links against the pdcurses.dll in the project directory without the need for the libpdcuses.dll.a..
if so, why all of the libname.dll.a files in .\lib ?
Correct. If you want to use the DLL, you don't need the .a file, nor do you need to distribute it.

The .a files in lib are in case you want to statically link the libraries into your exe so you don't need to bundle the dlls. Everything will be built into the exe. I think many FB users prefer static linking. There are some caveats to using static linking and some libraries like GTK+ don't support static linking at all. And sometimes there are license issues. DLLs have their advantages, especially if the dlls you want to use are already present on the system, such as is the case with Linux.
Or is it normal have both .\lib\libpdcusres.dll.a as well as .\project\pdcurse.dll
Yes many libraries will give you the static and dynamic version so you can decide how you want to use it.
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: Correct files and location to place sgared libraries

Post by Axle »

Reply to @caseih
I am confused, why are most (almost all) of the

Code: Select all

.\FreeBASIC-1.09.0-win64\lib\win64
named as

Code: Select all

lib[name].dll.a
if they are for static compile?
Wouldn't they just follow the normal lib archive convention of

Code: Select all

lib[name].a
?
Is FBC somehow packing the actual

Code: Select all

[name].dll
binary into the executable and unpacking it to memory as the

Code: Select all

[name].dll
at runtime and calling that static? (I have seen this trick done many times with AutoIt although they usually store it in a Base64 text format).


I tried compiling both raylib and PDCurses with just the

Code: Select all

raylib.dll
and the

Code: Select all

pdcurses.dll
in the project source directory and the example.bas compiled and ran without the need for any additional lib[name].dll.a files in .\FreeBASIC-1.09.0-win64\lib\win64, even though I was told previously that I needed the raylib.dll.a file to link against for implicit shared (As we would in C MinGW).

(P.S. Some people call "Implicit dynamic linking" or "Load-time dynamic linking" "Static linking" which it is not.)


So do I state that lib[name].dll.a is actually the equivalent of C (MinGW) Static library [name].a? (Static linking)
and FB will implicitly link to a [name].dll in the path without the definitions file the same as Linux? ( Load-time dynamic linking or implicit linking)


It is important that I get this correct as I am writing books to guide others through setting up libraries with FB (as well as C and Python3)

Example:
"As we are only using the dynamic library (Implicit dynamic linking) we do not require the archive [name.dll.a) for the linking symbols and only require the [name.bi] and the [name].dll in the compiler path."

Also another note, I am historically used to earlier versions on GCC and MinGW where the definitions from lib[name].dll.a where a requirement for implicit linking.. I think newer versions of GCC MinGW have dropped that requirement the same as for Linux. This may also be the case for FBC (v1.09.0) (winlibs-mingw-w64-x86_64-9.3.0-7.0.0-r3-sjlj) ?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Correct files and location to place sgared libraries

Post by dodicat »

For import libraries.
https://stackoverflow.com/questions/357 ... rk-details
(take.lib as the same as .a for gcc)
If you try to load a dll ,say
#inclib "zipfldr.dll"
which is listed nearly last in system 32, you wont get it at compile time, it is not listed in the lib folder of fb. (.dll.a)
But you can get it at run time

var l=dylibload ("C:\Windows\System32\zipfldr.dll")
print l
sleep

If you want this dll at compile time you should place it in the same folder as your .exe file calling it, and you don't need an import lib
lib zipfldr.dll.a.
You can create an import lib with dlltool though, if you wish.

The short and curly is these .dll.a files in the lib folder are methods of importing the real dll which contains the actual implementations of functions e.t.c.
Some of these .dll.a files have no corresponding dll (libbass.dll.a for example), you have to find the dll on the web.

Proper static libs (something.a) contain implementations, and are not just stubs.
If you create a dll yourself (-dll flag) then fb also makes an import lib (.dll.a).
You could then pop your dll into the system32 folder, and the .dll.a file into the fb lib folder.
But that would be a menacing way of using your dll, I have only tried it once in WinXp a few years ago.
I never mess about with the fb lib folder, or the system32 folder.
This would include the bass.dll I have mentioned, after you have found it.
Just use your dll as suggested above, i.e. in the same folder as your working .exe file, no need for the .dll.a file then.
Linux, I don't use, so I am unsure about the methods.
I am sure there are experts around who can embellish this answer.
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: Correct files and location to place sgared libraries

Post by Axle »

Thanks @dodicat

I was placing the libsomelib.dll.a into the FB .\lib folder on windows (implicit) but then I tried without it and it compiles fine so I am "guessing" FreeBASIC-1.09.0-winlibs-gcc-9.3.0 is now in line with Linux and can link implicitly to the libsomething.so, so the libsomething.dll.a is no longer required for Windows GCC MinGW as well.

So, I will run with just the DLLs in the project directory for a few examples and see how it goes.

I'll test this with my Windows C projects as also and see if I can now leave out the libsomething.dll.a and see if it implicitly links against the something.dll on its own. (GCC 9.2.0)

Appreciate the help.
Axle

P.S. the curses firework.c example works well on FB Win and Lubuntu. I had some trouble with the python curses versions not playing nice with QTerminal and the IDEs so I have dropped back to XTerm for curses dev in Linux. (They run ok when launched from the console in the default terminals though $ python3 ./firework.py)
Post Reply