Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distribute a program compiled with MinGW g++

Let's say I have created and compiled a simple program using the MinGW 64 (g++ compiler). Running this program on my computer and looking in Process Explorer for what DLL files the program is using I find (among many others):

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

These are the only ones that reside under my MinGW installation folder. The rest of the DLL files used reside under C:\Windows.

Question 1:

Are the MinGW DLL files the MinGW C++ runtime libraries (so to speak)? Do they serve the same purpose as for example msvcrXXX.dll (XXX = version of Microsoft runtime library).

Question 2:

If I want to run the application on a different computer which does not have MinGW installed, is it sufficient to include those DLL files listed above (i.e. placing them in the same folder as my executable) to have it run on the other computer (we assume the other computer is also a 64-bit Windows machine). If yes, does this mean we basically ship the MinGW C++ runtime with our executable. If no, why?

like image 425
jensa Avatar asked Jul 16 '15 08:07

jensa


People also ask

What is G ++ and MinGW?

MinGW is "Minimalist GNU for Windows", The GNU toolchain is a set of apps that helps with building applications (and more). In that toolchain there is the GNU compiler collection GCC. The c++ compiler within that collection is g++.

Does MinGW include G ++?

Mingw executables They also provide other compiler drivers, which in the case of mingw are executables such as g++, c++, g77, etc.

What is G ++ compiler?

g++ command is a GNU c++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file.

Can MinGW compile C code?

Install MinGW, a simple C compiler. If you haven't already done so, you'll need to install a C compiler on your PC to compile the C program. Compiling the C code will turn the code into an executable C program.


3 Answers

You may like to add the options -static-libgcc and -static-libstdc++ to link the C and C++ standard libraries statically and thus remove the need to carry around any separate copies of those.

like image 67
Hassen Dhia Avatar answered Sep 28 '22 08:09

Hassen Dhia


libstdc++6.dll is the C++ standard library, like you said.

libwinpthread-1.dll is for C++11 threading support. MinGW-W64 has two possible thread variants: Either use the native Windows functions like CreateThread, but C++11 stuff like std::thread won´t be available then; or include this library and use the C++11 classes (too).
Note that to switch the thread model, you´ll need to reinstall MinGW. Just removing the DLL and not using the C++11 stuff won´t work, the DLL will be required nonetheless with your current install.

libgcc_s_seh-1.dll is something about C++ exception handling.

Yes, it should be sufficient to deliver the DLLs too
(or use static linking and deliver only your program file).

like image 32
deviantfan Avatar answered Sep 28 '22 08:09

deviantfan


For complicated projects where you're not exactly sure which DLL files need to be included to distribute your application, I made a handy dandy Bash script (for MSYS2 shells) that can tell you exactly what DLL files you need to include. It relies on the Dependency Walker binary.

#!/usr/bin/sh

depends_bin="depends.exe"
target="./build/main.exe" # Or wherever your binary is
temp_file=$(mktemp)
output="dll_list.txt"

MSYS2_ARG_CONV_EXCL="*" `cygpath -w $depends_bin` /c /oc:`cygpath -w $temp_file` `cygpath -w $target`
cat $temp_file | cut -d , -f 2 | grep mingw32 > $output

rm $temp_file

Note that this script would need to be modified slightly for use in regular MSYS (the MSYS2_ARG_CONV_EXCL and cygpath directives in particular). This script also assumes your MinGW DLL files are located in a path which contains MinGW.

You could potentially even use this script to automatically copy the DLL files in question into your build directory as part of an automatic deploy system.

like image 45
Jacob Adamson Avatar answered Sep 28 '22 09:09

Jacob Adamson