I am using 3rd party libraries to build my executable. More specifically I have in ./extern/bin/win64
libfoo.dll
libfoo.dll.a
libbar.dll
Despite its name, it looks like libfoo.dll.a
isn't really an import library, but rather an archive (static library). After scratching my head for some time, I came to this conclusion by looking at the output of lib /list libfoo.dll.a
(from here) which gives me a bunch of .o files.
The command line I'm using to build my executable is always the same:
g++ -std=c++14 test.cpp -I./extern/include -L./extern/bin/win64 -lbaz -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic -lfoo -lbar -s -o test.exe
My understanding is that the final -Wl,-Bdynamic
gives preference to dynamic linkage but doesn't insist on dynamic linkage. The linker should still be happy if only the static version of a library exist, but it should pick the dynamic version when both the static and the dynamic version exist. Correct ?
Now here is the part that puzzles me a little bit. If, in ./extern/bin/win64
, I only keep libbar.dll
and:
libfoo.dll
then the executable has dependencies onlibfoo.dll
andlibbar.dll
(expected: dynamic linkage tolibfoo
is the only option)libfoo.dll.a
then the executable has a dependency onlibbar.dll
only (expected: static linkage tolibfoo
is the only option)- Both
libfoo.dll
andlibfoo.dll.a
then the executable has a dependency onlibbar.dll
only (surprising: static linkage seems to have been used but dynamic linkage was supposed to be preferred)
Does anyone have a theory to explain #3 ? Are my expectations incorrect ?
[Edit] As an extension to #3, I have also found that if I separate libfoo.dll
and libfoo.dll.a
into different folders, then the first folder to appear on the command line following -L
determines which version of the library is selected. Again -Wl,-Bdynamic
does not seem to have the expected effect....
PS: if anyone wants to have a detailed inspection of the libraries, they're distributed with Matlab, located in extern\bin\win64
as discussed. For completeness, the source code for my example can be found here but is this probably not relevant. Also using gcc 6.3 from here.