我有一个简单的QT项目.我正在
Linux上开发.但它最终要部署到Linux,Mac和Windows.
我正在尝试打包它以便分发.我遇到了查找依赖项并将其打包并以惯用方式执行此操作的问题(IOW:没有硬编码的DLL路径或包含我的源代码库中的DLL)
对于Windows端口,我正在使用MinGW并进行如下编译:
mingw64-cmake -G "Unix Makefiles" .. -DCMAKE_INSTALL_PREFIX=../install -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=/usr/share/mingw/toolchain-mingw64.cmake make && ctest && make install && cpack -G "TGZ" && cpack -G "NSIS64"
我已将其设置为生成tar.gz文件和NSIS安装程序.目前没有特别的理由让NSIS而不是Wix.这只是为了解决问题.
它编译Windows可执行文件,但它不包括运行该程序所需的DLL.就是这些:
Libgcc_s_seh-1.dll Qt5Core.dll Qt5Gui.dll
/usr/x86_64-w64-mingw32/sys-root/mingw/bin/Qt5Widgets.dll /usr/x86_64-w64-mingw32/sys-root/mingw/bin/libgcc_s_seh-1.dll ...
有没有办法自动让CPack挖掘DLL并将它们包含在安装程序中?
这是我的CMakeLists.txt文件:
cmake_minimum_required(VERSION 2.8.11) project(myapp) enable_testing() set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) find_package(Qt5Core required) find_package(Qt5Gui required) find_package(Qt5Widgets required) add_executable(myapp WIN32 main.cpp mainwindow.cpp resources.qrc) target_link_libraries(myapp Qt5::Widgets) target_link_libraries(myapp Qt5::Core) target_link_libraries(myapp Qt5::Gui) INSTALL(TARGETS myapp BUNDLE DESTINATION . RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib ) INSTALL(FILES ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION bin COMPONENT Libraries) IF(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS) INSTALL(PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION bin COMPONENT System) ENDIF(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS) INCLUDE(CPack)
我四处寻找一些帮助.我遇到的最好的事情是
this link
但它看起来并不那么惯用.如果我们仔细观察CMakeLists.txt文件,它将获得特定于机器的硬编码路径,这些路径将来肯定会发生变化:
IF( WIN32 AND ${ARCH_32BIT}) SET(QT_INSTALLED_PATH "C:/QtMSVCX86/Qt5.5.0/5.5/msvc2013" ) ELSEIF(WIN32 AND ${ARCH_64BIT}) SET(QT_INSTALLED_PATH "C:/QtMSVCX64/Qt5.5.0/5.5/msvc2013_64" ) ELSEIF(UNIX AND NOT MINGW AND ${ARCH_32BIT}) SET(QT_INSTALLED_PATH "/opt/Qt5.5.0/5.5/gcc/" ) ELSEIF(UNIX AND NOT MINGW AND ${ARCH_64BIT}) SET(QT_INSTALLED_PATH "/opt/Qt5.5.0/5.5/gcc_64/" ) ENDIF() SET(CMAKE_AUTOMOC ON) SET(CMAKE_AUTOUIC ON) SET(CMAKE_AUTORCC ON) FIND_PACKAGE(Qt5Widgets PATHS ${QT_INSTALLED_PATH} NO_DEFAULT_PATH) FIND_PACKAGE(Qt5Qml PATHS ${QT_INSTALLED_PATH} NO_DEFAULT_PATH) FIND_PACKAGE(Qt5Quick PATHS ${QT_INSTALLED_PATH} NO_DEFAULT_PATH)
解决方法
看看CMake的
Bundle Utilities它包含一个FIXUP_BUNDLE宏,它收集可执行文件的所有必要依赖项,包括Qt.它在Windows,Linux和Mac上的工作方式基本相同.您可以首先将FIXUP_BUNDLE(myapp)添加到CMake文件中.在CPack运行期间发生了实际的依赖关系解析和复制.根据项目规模和复杂程度,需要进行一些调整,但总的来说,我已经看到它在基于CMake的构建系统的大型跨平台Qt项目中成功使用