3

I am using msys2 to compile a library that uses autotools as the build system. The end result is a DLL. This DLL ends up referring to the following other DLLs that come with msys2:

  • libgcc_s_seh-1.dll
  • libstdc++-6.dll
  • libwinpthread-1.dll

How can I link these statically and eliminate these dependencies?

There are other questions dealing with this (example) and the solutions suggest using the options -static-libgcc -static-libstdc++. These work when linking an .exe, but they do not seem to work when linking a .dll.

I set the following variables before running ./configure (and checked the output to verify that these compiler options are really being used), but Dependency Walker still shows a dependency on libstdc++-6.dll, just as before.

export CFLAGS="-static-libgcc -static-libstdc++" CXXFLAGS="-static-libgcc -static-libstdc++" LDFLAGS="-static-libgcc -static-libstdc++"

(I assume these must only go in LDFLAGS, but since I don't have a full understanding, I also added them in CFLAGS and CXXFLAGS.)

Is there a way to get rid of these dependencies when linking a DLL, not an EXE?

The library is written in a mix of C and C++ and has a C API.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174

1 Answers1

1

Try just using the -static option in LDFLAGs. I tested it just now in MSYS2 and it worked for me.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Did you test it when creating a DLL or an EXE? It appears to prevent the creation of a DLL. – Szabolcs Jun 02 '20 at 19:10
  • You're right, it works on a minimal example. I need to figure out why it does not work when compiling this library. – Szabolcs Jun 02 '20 at 19:25
  • Note that when using `-static` you also need to link with the dependancies of the dependancies, and with their dependancies and so on. If the libraries come with `.pc` files this can be solved by linking with the output of `pkg-config --static --libs ` – Brecht Sanders Jun 04 '20 at 06:14
  • I don't think so. We are still making a DLL, not a static library (.a), and DLLs include references to the DLLs they depend on. – David Grayson Jun 04 '20 at 07:46
  • @DavidGrayson Funny thing about Windows is that it is possible to make a DLL that statically links with its dependancies. So `-static` can be combined with `-o -Wl,--out-implib,.a` which you can then combine with `$(pkg-config --static --libs )` as mentioned earlier. – Brecht Sanders Jun 04 '20 at 13:07
  • In your example, you shouldn't need to use `--static` but I guess it depends on whether the authors of the .pc file did a good job in defining the .pc file. DLLs do not depend on .a files after they have been created because DLLs are a thing that the OS knows how to run, and .a is a compiler file format. If you can make a [Gist](https://gist.github.com/) or a blog post or something that demonstrates your point using a precise sequence of MSYS2 shell commands, we could discuss it there, instead of cluttering up this question. – David Grayson Jun 04 '20 at 17:32