CMake:构建跨平台分发 [英] CMake: build cross-platform distribution
问题描述
在我的MacOSX上,我开发了一个使用Qt和VTK库的应用程序。我使用CMake生成makefile。
On my MacOSX I've developed an application that makes use of Qt and VTK libraries. I generate the makefile using CMake.
现在我想在Windows上编译一个最终用户自包含的包,它应该在最终用户机器上工作需要预安装Qt或VTK库。我认为可以通过修改CMakeLists.txt文件,但网页搜索没有指出我的方向。
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.
如何使Windows的可分发包使用CMake?
How to make a distributable package for Windows using CMake?
推荐答案
我在一个自己的项目中做了一个小脚本,文件或.tll文件从VTK的cmake变量和QT_LIBRARIES变量。
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.
之后,我将这些.dll或.so文件添加到我的安装目标(下面的示例脚本),安装目标将复制那些.dll或.so文件将VTK_DIR或QTDIR转换为$ {CMAKE_INSTALL_PREFIX} \bin。这与CPack兼容,所以你可以写一个小的cpack脚本。
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.
注意,然而,你需要一个更多的Windows上获得一个最终用户自包含软件包:您还需要系统库(msvcrt.dll,msvcrp.dll或mingwm10.dll,libstdc ++。dll)。例如。请查看此问题。
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.
在Windows上,以下脚本从VTK_DIR中查找所有 Vtk dll。
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 )
对于Qt,脚本有一点长,因为我需要找到调试库和发布库。上方:它只搜索我请求的组件 find_package(Qt4 ...)
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 )
这篇关于CMake:构建跨平台分发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!