I've compiled a C++ program and it's perfectly working on my computer, but if my friend tries to launch the program, it says libgcc_s_sw2-1.dll
is missing. How I can include all the required GCC runtime libraries with the program using CMake?
Asked
Active
Viewed 5,332 times
7

Smi
- 13,850
- 9
- 56
- 64

Vlad Markushin
- 443
- 1
- 6
- 24
2 Answers
10
As rubenvb correctly answers: libgcc is required or you should add CMAKE_EXE_LINKER_FLAGS=-static
to your CMakeLists.txt.
As an alternative, you could try to find libgcc_s_sw2-1.dll in your MinGW installation and "package" it with your installation using InstallRequiredSystemLibraries. This integrates nicely with CPack as well.
E.g. in my own code, I have:
if( MINGW )
message( STATUS " Installing system-libraries: MinGW DLLs." )
get_filename_component( Mingw_Path ${CMAKE_CXX_COMPILER} PATH )
set( CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS ${Mingw_Path}/mingwm10.dll ${Mingw_Path}/libgcc_s_dw2-1.dll ${Mingw_Path}/libstdc++-6.dll )
endif( MINGW )
include( InstallRequiredSystemLibraries )
Later on, in the part which prepares an install or package:
# Actually install it when make install is called.
# Note, this works with CPack
if( CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS )
install( PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION bin COMPONENT System )
endif( CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS )

Nathan Osman
- 71,149
- 71
- 256
- 361

André
- 18,348
- 6
- 60
- 74
-
1note that MinGW-w64 doesn't have `mingwm10.dll`, and they generally provide `libgcc_s_sjlj-1.dll`, and GCC 4.8 for x64 will have a `libgcc_s_seh-0.dll` by default. The automatic packaging of libgcc is very build system dependent. – rubenvb Oct 21 '12 at 15:30
-
1@rubenvb. True and I think your solution is the better one. But in those cases where a user does want to build against libgcc as a dll, at least with CMake it is possible to collect all those system dependent files. In my own project, I have even written a little script which does this for MinGW, VS2008 and VS2010. – André Oct 21 '12 at 17:53
5
The libgcc dll is required by all programs compiled with GCC. If you don't want to redistribute this DLL with your program, you must link statically by passing -static
to the linker, or in CMake:
CMAKE_EXE_LINKER_FLAGS=-static
This is specific for GCC.

rubenvb
- 74,642
- 33
- 187
- 332