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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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 )
回答2:
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.