Last active
January 31, 2024 12:57
-
-
Save Rod-Persky/e6b93e9ee31f9516261b to your computer and use it in GitHub Desktop.
Example cmake for windows including auto copy dll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# _______ __ __ _______ ______ _______ _______ _______ ______ # | |
#| || | | || || | | _ || || || | # | |
#| _ || | | ||_ _|| _ || |_| ||_ _|| ___|| _ |# | |
#| | | || |_| | | | | | | || | | | | |___ | | | |# | |
#| |_| || | | | | |_| || | | | | ___|| |_| |# | |
#| || | | | | || _ | | | | |___ | |# | |
#|_______||_______| |___| |______| |__| |__| |___| |_______||______| # | |
# # | |
# Modern CMake practices and importing the QT scripts by adding it to # | |
# your module path makes things a lot better than it used to be # | |
cmake_minimum_required(VERSION 2.8.11) | |
project(testproject) | |
set(CMAKE_PREFIX_PATH "C:/Qt/5.3/msvc2013_64/lib/cmake") | |
set(CMAKE_INCLUDE_CURRENT_DIR ON) | |
set(CMAKE_AUTOMOC ON) | |
# Find the QtWidgets library | |
find_package(Qt5Widgets REQUIRED) | |
find_package(Qt5Core REQUIRED) | |
find_package(Qt5Gui REQUIRED) | |
QT5_WRAP_UI(UI_HEADERS mainwindow.ui) | |
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} ) | |
# Tell CMake to create the helloworld executable | |
add_executable(helloworld main.cpp mainwindow.cpp ${UI_HEADERS}) | |
target_link_libraries(helloworld Qt5::Widgets) | |
add_custom_target(Qt5CopyBinaries | |
# todo: check if debug and release folder exist | |
# debug version | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/libEGLd.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/libGLESv2d.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Cored.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Guid.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Declaratived.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Networkd.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5OpenGLd.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Scriptd.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Sqld.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Widgetsd.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Xmld.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5XmlPatternsd.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/icuin52.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/icuuc52.dll ${CMAKE_BINARY_DIR}/Debug | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/icudt52.dll ${CMAKE_BINARY_DIR}/Debug | |
# release version | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/libEGL.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/libGLESv2.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Core.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Gui.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Declarative.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Network.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5OpenGL.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Script.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Sql.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Widgets.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5Xml.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/Qt5XmlPatterns.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/icuin52.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/icuuc52.dll ${CMAKE_BINARY_DIR}/Release | |
COMMAND ${CMAKE_COMMAND} -E copy ${Qt5Core_DIR}/../../../bin/icudt52.dll ${CMAKE_BINARY_DIR}/Release | |
# Output Message | |
COMMENT "Copying Qt binaries from '${Qt5Core_DIR}/../../bin/' to '${CMAKE_BINARY_DIR}'" VERBATIM | |
) | |
add_dependencies(helloworld Qt5CopyBinaries) |
Here are some further changes to the add_custom_command part:
- Copy Debug and Release dlls only when the according build configuration is selected
- Get the dll file paths through ...Config.cmake files
- cmake -E copy_if_different instead of cmake -E copy
# find the release *.dll file
get_target_property(Qt5_CoreLocation Qt5::Core LOCATION)
# find the debug *d.dll file
get_target_property(Qt5_CoreLocationDebug Qt5::Core IMPORTED_LOCATION_DEBUG)
#
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<$<CONFIG:Debug>:${Qt5_CoreLocationDebug}> $<$<NOT:$<CONFIG:Debug>>:${Qt5_CoreLocation}> $<TARGET_FILE_DIR:MyApp>)
Rewrited as macros
macro(qt5_copy_dll APP DLL)
# find the release *.dll file
get_target_property(Qt5_${DLL}Location Qt5::${DLL} LOCATION)
# find the debug *d.dll file
get_target_property(Qt5_${DLL}LocationDebug Qt5::${DLL} IMPORTED_LOCATION_DEBUG)
add_custom_command(TARGET ${APP} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<$<CONFIG:Debug>:${Qt5_${DLL}LocationDebug}> $<$<NOT:$<CONFIG:Debug>>:${Qt5_${DLL}Location}> $<TARGET_FILE_DIR:${APP}>)
endmacro()
qt5_copy_dll(MyApp Core)
My solution to using app windeployqt
instead
if (WIN32)
get_target_property(QT5_QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)
get_filename_component(QT5_WINDEPLOYQT_EXECUTABLE ${QT5_QMAKE_EXECUTABLE} PATH)
set(QT5_WINDEPLOYQT_EXECUTABLE "${QT5_WINDEPLOYQT_EXECUTABLE}/windeployqt.exe")
add_custom_command(TARGET foot_scanner POST_BUILD
COMMAND ${QT5_WINDEPLOYQT_EXECUTABLE} --qmldir ${CMAKE_SOURCE_DIR} $<TARGET_FILE_DIR:foot_scanner>)
endif(WIN32)
This could be simplified quite a bit with
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:Qt5::Core>
$<TARGET_FILE:Qt5::Widgets>
... etc ...
instead
So now I have all the right files in one place. But I still want things copied upon INSTALL. How would I achieve that?
.... elegantly, that is.
This could be simplified quite a bit with
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Core> $<TARGET_FILE:Qt5::Widgets> ... etc ...
instead
How can I link opencv dll like this?
Beware that qml-related dependencies aren't covered by this script, and the app will fail to run, unless you use windeployqt.exe
to copy all necessary modules.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see you have a TODO in your file. What you can do is to do a custom_command instead of a custom target and execute it after building is done, with that you avoid checking if Debug/Release exists and the add_dependencies is not needed anymore. Here is what I did
Hope that helps