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?

1 Answer 1

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

2
  • 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 8, 2021 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 but don’t appear in the --help output. Feb 8, 2021 at 15:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .