CMake: build cross-platform distribution

自作多情 提交于 2020-02-21 05:58:08

问题


On my MacOSX I've developed an application that makes use of Qt and VTK libraries. I generate the makefile using CMake.

Now I want to compile an end-user self-contained package on Windows, and it is supposed to work on end-user machine without needing to pre-install Qt or VTK libraries. I think is possible to do this by modifying the CMakeLists.txt file but a web search hasn't pointed me the right direction.

How to make a distributable package for Windows using CMake?


回答1:


What I have done in one of my own projects is write a little script which will give me the .so files or .dll files from VTK's cmake-variables and QT_LIBRARIES variables.

After that, I add those .dll or .so files to my install targets (example scripts below) and the install target will copy those .dll or .so files from the VTK_DIR or QTDIR into ${CMAKE_INSTALL_PREFIX}\bin. This is compatible with CPack, so you could write a little cpack-script too.

Note, however, that you need a little more on windows to get an end-user self-contained package: You will also need the "system-libraries" (msvcrt.dll, msvcrp.dll or mingwm10.dll, libstdc++.dll). E.g. take a look at this question.

On windows, the following scripts finds all Vtk dlls from the VTK_DIR.

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 )

And for Qt the script is a little longer, because I needed to find both debug- and release libraries. The up-side: It only searches for those components I requested with 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 )    


来源:https://stackoverflow.com/questions/10022993/cmake-build-cross-platform-distribution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!