rsk82
rsk82

Reputation: 29407

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

I use mingw from here: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.7.2/32-bit/threads-posix/sjlj/x32-4.7.2-release-posix-sjlj-rev2.7z/download

And I've sucessfully managed to link statically libstdc++-6.dll and libgcc_s_sjlj-1.dll by using -static-libgcc -static-libstdc++ parameters, but I cannot find a command for doing the same with libwinpthread-1.dll.

Upvotes: 70

Views: 88997

Answers (10)

GoDaft WithEBK
GoDaft WithEBK

Reputation: 1

If you are using toolchains from MSys2 (tested with gcc version 12) ,the only way to use winpthread statically is standard -static (forces all library to be static) or simply delete/rename the libwinpthread.dll.a && libpthread.dll.a imp libs. Other methods like "Bstatic", "-l:libwinpthread.a" will no longer work (due to dependency hell, especially in CMake). The one with whole archive still works but it overrides the program properties. You may also want static link libgcc and libstdc++ if the dependency comes from them.

Upvotes: 0

superbem
superbem

Reputation: 479

Just link with -l:libwinpthread.a

Upvotes: 0

Hermann
Hermann

Reputation: 636

I circumvented this problem by using win32 variant of the mingw toolchain instead of the posix variant. With the win32 variant, -static-libgcc -static-libstdc++ is sufficient for an independent build.

Upvotes: 1

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.

Upvotes: 7

user7023624
user7023624

Reputation: 611

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.

Upvotes: 29

Syndog
Syndog

Reputation: 1677

For anyone working in CMake, this solution is readily implemented in your CMakeLists.txt file as follows...

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")

Upvotes: 11

Star Brilliant
Star Brilliant

Reputation: 3136

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.

Upvotes: 41

Neobry
Neobry

Reputation: 13

Just add -static to your CFLAGS.

For example: ./configure CFLAGS="-static".

This will link all static libraries to your executable file.

Upvotes: -2

rubenvb
rubenvb

Reputation: 76785

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++.

Upvotes: 66

Tomasz Lis
Tomasz Lis

Reputation: 312

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".

Upvotes: 28

Related Questions