Compile C++ Windows exe standalone files with MSYS2

Viewed 24

I've recently started using Msys2 to install gcc compiler to make some exe for Windows. It works very well, but there's a problem when passing my exe to my brother. His laptop has not msys2 installed and when he tries to run my exe some errors occur. Seems like few dll files are necessary to use my exe (like msys-2.0.dll).

I've found out that those files are used by msys2 to "fake" the OS on the machine pretending it's a POSIX one. Is there a way to compile standalone exe for windows with msys2? I would like my brother to be able to use my exe without installing msys or else.

Here are all the details to understand better my situation:

  1. g++ HelloWord.cpp -o Helloword is the line I use to compile
  2. C:\msys64\mingw64\bin here's the path where g++ is stored
  3. All the exact error messages I receive from windows after double clicking on the exe file that has been generated. Note that these messages do not appear on the CMD, but in a classic Windows error pop-up:
  • The program can't start because msys-2.0.dll is missing from your computer. Try reinstalling the program to fix this problem.
  • The program can't start because libstdc++-6.dll is missing from your computer. Try reinstalling the program to fix this problem.
  • The program can't start because libgcc_s_seh-1.dll is missing from your computer. Try reinstalling the program to fix this problem.

Fixed: I've resolved the issue just using the g++ parameter -static. Is it an overkill?

2 Answers

My version of MinGW is a bit old ...

C:\example>where g++
  C:\misc\mingw810_64\bin\g++.exe

C:\example>g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

But same idea:

C:\example>cat > compile_me.cpp
#include <iostream>
int main () { std::cout << "hi" << std::endl; }
^Z

C:\example>g++ compile_me.cpp -o compiled.exe

C:\example>compiled.exe
hi

C:\example>dumpbin /dependents compiled.exe

  ...

  Image has the following dependencies:

    KERNEL32.dll
    msvcrt.dll
    libstdc++-6.dll
  
  ...

In that case (dynamically linked stdlib) you'd deploy libstdc++6.dll with the executable, installing it to the same path as the exe (the other two are generally present in the windows system path).

If you want to drop that dependency, use -static:

C:\example>g++ compile_me.cpp -o compiled.exe -static

C:\example>compiled.exe
hi

C:\example>dumpbin /dependents compiled.exe

  ...

  Image has the following dependencies:

    KERNEL32.dll
    msvcrt.dll

  ...

Deploying that .exe alone should be fine.

The file size will be larger but that's not a huge deal these days. Also your MinGW / MSYS install might come with strip:

C:\example>dir compiled.exe
 Volume in drive C is Windows
 Volume Serial Number is D2BA-C6F0

 Directory of C:\example

09/24/2022  06:49 PM         2,389,120 compiled.exe
               1 File(s)      2,389,120 bytes
               0 Dir(s)  135,945,314,304 bytes free

C:\example>strip compiled.exe

C:\example>dir compiled.exe
 Volume in drive C is Windows
 Volume Serial Number is D2BA-C6F0

 Directory of C:\example

09/24/2022  07:03 PM           838,656 compiled.exe
               1 File(s)        838,656 bytes
               0 Dir(s)  135,944,765,440 bytes free

C:\example>compiled.exe
hi

If there are other dynamic libraries that your particular executable ends up depending on, and the vendor has chosen not to provide statically linked alternatives, then you'll have to just deploy them with the exe. It's generally easy enough to just throw everything in a zip file or use your favorite scriptable installer.

(Note: dumpbin ships with Visual Studio; and can be found in some appropriate subdirectory in VC\Tools in the vs install path).

MSYS2 compiles native PE32 executables. It does not rely on any magic msys environment or static linking.

Get yourself a dependency walker and look to see what DLLs your app needs to run. Anything not in a Windows subdirectory should be where you focus your attention. Also make sure your app does not require any special Microsoft redistributable dependencies.

Ultimately, you should be creating an installer for your application to handle dependencies. I personally like Inno setup, but plenty of others exist that are well liked also.

Related