CMake的:构建跨平台分发(CMake: build cross-platform distrib

2019-07-28 19:27发布

在我的MacOSX我开发的应用程序,使得使用Qt和VTK库。 我生成使用CMake的makefile文件。

现在我想在Windows上编译最终用户的自包含的包,它应该无需预先安装的Qt和VTK库的最终用户的机器上工作。 我认为这是有可能通过修改的CMakeLists.txt文件,但在网上搜索也没有向我指出了正确的方向来做到这一点。

如何使用CMake的制作分发包的Windows?

Answer 1:

我在我自己的一个项目做的是写一个小脚本,这将给我的.so文件,或.dll文件从VTK的CMake的变量和QT_LIBRARIES变量。

在那之后,我那些的.dll或.so文件添加到我的安装目标(下面的示例脚本),并安装目标将那些.dll文件或从VTK_DIR或QTDIR .so文件复制到$ {} CMAKE_INSTALL_PREFIX \ BIN。 这是CPack兼容,所以你可以写一个小cpack脚本了。

但是请注意,你需要多一点的窗户让最终用户自包含包 :您还需要在“系统库”(MSVCRT.DLL,msvcrp.dll或mingwm10.dll,的libstdc ++ DLL)。 。 例如,看看这个问题 。

在Windows上,下面的脚本发现从VTK_DIR 所有 VTK的DLL。

file( GLOB VTK_DLLS ${VTK_RUNTIME_LIBRARY_DIRS}/*.dll )
if( VTK_DLLS )
    foreach( Vtk_library ${VTK_DLLS} )
        # Add it to the list of 'desired' vtk-libraries for later installation
        list( APPEND Vtk_Install_Libraries ${Vtk_library} )
    endforeach( Vtk_library ${VTK_DLLS} )
    list( REMOVE_DUPLICATES Vtk_Install_Libraries )
    install( FILES ${Vtk_Install_Libraries} DESTINATION bin COMPONENT ThirdParty  )
endif( VTK_DLLS )

而对于Qt的脚本是长一点,因为我需要找到既调试 - 并释放库。 向上的一面:它只能搜索我请这些组件find_package( Qt4 ... )

# If Qt-4 was used, add the 'found' Qt-libraries to the Install-target.
if ( USE_QT )
    foreach( Qt_library ${QT_LIBRARIES} )
        # With QT_USE_IMPORTED_TARGETS, we should extract the dll info 
        # from the target properties
        get_target_property( Qt_lib_name ${Qt_library} IMPORTED_LOCATION )
        get_target_property( Qt_lib_name_debug ${Qt_library} IMPORTED_LOCATION_DEBUG )
        get_target_property( Qt_lib_name_release ${Qt_library} IMPORTED_LOCATION_RELEASE )

        # Initially assume the release dlls should be installed, but 
        # fall back to debug if necessary
        if ( Qt_lib_name_release AND EXISTS ${Qt_lib_name_release} )
            set( Qt_library_location ${Qt_lib_name_release} )
        elseif ( Qt_lib_name_debug AND EXISTS ${Qt_lib_name_debug} AND ENVIRONMENT_DEBUG )
            set( Qt_library_location ${Qt_lib_name_debug} )
        elseif ( Qt_lib_name AND EXISTS ${Qt_lib_name} )
            set( Qt_library_location ${Qt_lib_name} )
        endif ( Qt_lib_name_release AND EXISTS ${Qt_lib_name_release} )

        # Extract the filename part, without the lib-prefix or the .a or ..lib suffix
        get_filename_component( Qt_library_name ${Qt_library_location} NAME_WE )
        string( REGEX REPLACE "^lib(.*)" "\\1" Qt_library_name ${Qt_library_name} )

        set( Qt_shared_library ${QT_BINARY_DIR}/${Qt_library_name}.dll )
        if ( EXISTS ${Qt_shared_library} )
            # Add it to the list of 'desired' qt-libraries for later installation
            list( APPEND Qt_Install_Libraries ${Qt_shared_library} )
        else ( EXISTS ${Qt_shared_library} )
            message( WARNING "    could not find ${Qt_shared_library}" )
        endif ( EXISTS ${Qt_shared_library} )
    endforeach( Qt_library ${QT_LIBRARIES} )
    # When building against a static Qt, the list of Qt_Install_Libraries can be empty
    if ( Qt_Install_Libraries )
        list( REMOVE_DUPLICATES Qt_Install_Libraries )
        install( FILES ${Qt_Install_Libraries} DESTINATION bin COMPONENT ThirdParty )
    endif ( Qt_Install_Libraries )
endif ( USE_QT )    


文章来源: CMake: build cross-platform distribution