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) |
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
@aaichert , I don't know if this qualifies as 'elegant' for you, but I found this code that works for me. I put it in a macro that I use for my apps.