how to do static linking of libwinpthread-1.dll in mingw?

If your toolchain includes the static winpthreads, adding the option

-static

Will pull in static versions of all libraries it can.

Alternatively, you can remove libwinpthread.dll.a and the DLL itself from the toolchain directories. This might mess up programs linking with libstdc++ and libgcc DLLs though, so be careful.

A third option is to use -Wl,-Bdynamic and -Wl,-Bstatic to select which version you want linked in (which is what -static internally does when ld is called). An example:

gcc -o someexec someobject.o -Wl,-Bdynamic -lsomelibIwantshared -Wl,-Bstatic -lsomelibIwantstatic

If you run your link command with -v added, you should see these options appearing in the ld/collect2 invocation when you use -static-libgcc and -static-libstdc++.


You should probably check command line options documentation for GCC.

These's no '-static-something' command, only standard libraries (libgcc and libstdc++) can be set to static linking with one command. For other libraries, you first switch to static linking with "-static" and then list the libraries to include with separate commands, ie "-lpthread".


To statically link winpthread even if threading isn't used in the program, pass the -Bstatic and --whole-archive parameters to the linker:

g++ -o hello.exe hello.cpp -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive

Note the following:

  • The "whole archive" option should be disabled immediately afterwards.
  • You don't need to do this hack if your program actually uses symbols from the library (i.e. you use <thread> from C++11), in which case the library won't get dropped when you statically link it.
  • This hack is intended for MinGW-w64, to fix the libwinpthread-1.dll dependencies.

Try this:

-static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic

Notice the -lstdc++ before -lpthread. It worked for me.

Make sure to add this to the very end of your g++ command line.