Apparently, CMake does some weird stuff with how the -Wl
compiler flags are handled, making the -Wl,-Bstatic -lstdc++ -lwinpthread -Wl,-Bdynamic
solution not work, with only two other options seemingly left: the bad compiler flag -static
and the ugly compiler flag -Wl,--whole-archive
.
Meanwhile, the good option that actually works in CMake, yet seems rather undocumented, is to directly use the linker flags. Hence, in CMake, this seems to be the best way to statically link to all the mingw-w64 C++ dependencies:
target_link_libraries (MyVeryAwesomeApp -static gcc stdc++ winpthread -dynamic)
It should be noted that even if there isn't a library explicitly following -dynamic
, it should still be applied in order to ensure that the standard, implicitly linked libraries get linked correctly.