How can I install GCC runtime libraries with CMake

2019-03-31 03:06发布

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?

标签: gcc cmake mingw
2条回答
Viruses.
2楼-- · 2019-03-31 03:15

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 )
查看更多
叛逆
3楼-- · 2019-03-31 03:33

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.

查看更多
登录 后发表回答