10

I want to compile a simple Windows application on Linux using MinGW.

I compile as follows:

x86_64-w64-mingw32-g++ -L. -l:mathlib.dll -o fib main.cpp mathlib.h

This results in an executable fib that computes the N'th fibonacci number using a function imported from mathlib.dll.

Now, when executing fib on Windows, it complains that libgcc_s_seh-1.dll and libstdc++-6.dll are missing.

If I copy these DLLs to Windows, then everything works, but I'd like not to copy these MinGW dependencies with every executable.

How can I statically link these MinGW depencies, such that foo depends on no other DLLs than mathlib.dll?

Shuzheng
  • 4,023
  • 1
  • 31
  • 71

1 Answers1

11

Build your program (and mathlib.dll, if necessary) with the -static-libgcc and -static-libstdc++ options:

x86_64-w64-mingw32-g++ -static-libgcc -static-libstdc++ -L. -l:mathlib.dll -o fib main.cpp mathlib.h

This will produce a binary with no external dependencies on libgcc and libstdc++.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • Thank you. It works. However, I also had to compile the DLL with those options, otherwise I got an error for `fib.exe`, and it was not obvious that the error originated from the DLL (in fact nothing was mentioned about `mathlib.dll` in the error message). What `gcc` command do use to see those options (e.g. `static-libgcc`)? Nothing is shown for `gcc -v --help | grep static-libgcc`. – Shuzheng Feb 08 '21 at 15:11
  • Yes, `mathlib.dll` would acquire its own dependencies on those DLLs if you build it using GCC without those options (I hadn’t realised you were building the DLL too). The options [are documented in the manual](https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html) but don’t appear in the `--help` output. – Stephen Kitt Feb 08 '21 at 15:15