Extensive googling does not reveal how to build a fully static binary (EXCEPT for Microsoft Windows DLLs) using MinGW-W64 compilers. Out of the several things people have created, none seem to work reliably.
I've seen passing flags to the C/C++ compiler
-static-libgcc -static-libstdc++
and although these work, similar flags do not exist for the other linked in libraries (e.g. libwinpthreads-1.dll)
Also hacks by passing arguments to the compiler from the driver do not seem to always work. I've seen
-static'-Wl,-Bstatic -lwinpthread -Wl,-static-Wl,-Bstatic -lstdc++ -lpthread -Wl,-static`
and various so-called hacks using
-Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive
So, what is the proper way to build a fully static binary (exe or dll) that only depends on Microsoft Windows DLLs?
Basically the -static linker flag is what you need.
Use the additional -static-libgcc and -static-libstdc++ flags to use also the static standard library.
For example example, if you have these files in your library path (e.g. via -L linker flag):
the following command will build use the static library (libz.a):
and this will use the shared library (libz.dll.a):
It's important to note that shared libraries will load there dependancy DLLs at runtime, while static libraries must have all their dependancies linked in at link time.
For example if libpng depends on libz and your program calls functions in libpng you will need to also add the dependancy library libz, otherwise the linker will give you errors about missing symbols:
You can use
-static-libstdc++ -static-libstdc++but I haven't had success combining that with
-lwinpthread: a missing dependency always popped up saying I needed *libwinpthread-1.dll.I'm using a posix cross compiler build of mingw-w64 v9.0.0/gcc version 12.2.0 on linux.
After several attempts to correctly configure dosbox to use the static libraries of stdc++, gcc, and winpthreads, I was able to successfully build dosbox using the LIBS variable with the following setting:
LIBS="-Wl,-Bstatic -lstdc++ -lgcc -lgcc_eh -lwinpthread -Wl,-Bdynamic"
I found that this formula successfully eliminated the dependencies on the dynamic libraries of stdc++, gcc, and winpthreads.
Note that gcc_eh was also added to the list of static libraries. I found that in the Make_cyg_ming.mak for a vim 9.x.xxxx version, they added this to list of static libraries if STATIC_STDCPLUS was defined on their custom Makefile for vim. This eliminates the message saying I need a libgccsseh-1.dll
Also, note that no space occurs after the
-Wl,declaration. I found that if I added a space after the comma--as I have seen in many examples on the internet--, my configure options would be rejected by the configure script saying that the C compiler did not work. Maybe the spaces weren't there, but it appeared that they had a space--at least to me.So, in essence, to use the static libraries with your compiler, if it supports both static and dynamic libraries, use the following formula: "
-Wl,-Bstatic <libraries you wish used as static> -Wl,-Bdynamic"Last edit: Henry Arthur Garcia 2023-04-30
Use
-static-libstdc++/-static-libstdc++in combination with-staticso not only standard libraries are statically linked, but also any others (like-lwinpthread).