QT6 Clion

vx_guanchaoguo0發表於2024-11-25
cmake_minimum_required(VERSION 3.29)
project(untitled2)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH "C:/Qt/6.8.0/mingw_64/lib/cmake")

find_package(Qt6 COMPONENTS Core Gui Widgets OpenGL OpenGLWidgets REQUIRED)

set(OpenCV_DIR "C:/Qt/6.8.0/mingw_64/lib/cmake/OpenCV/x64/mingw/lib")
set(OpenCV_BIN_DIR "C:/Qt/6.8.0/mingw_64/lib/cmake/OpenCV/x64/mingw/bin")

find_package(OpenCV REQUIRED)

set(GDCM_LIB_DIR "C:/Qt/6.8.0/mingw_64/lib/cmake/GDCM/lib")
set(GDCM_INCLUDE_DIRS "C:/Qt/6.8.0/mingw_64/lib/cmake/GDCM/include/gdcm-3.0")

set(GDCM_LIBS
        ${GDCM_LIB_DIR}/gdcmDICT.lib
        ${GDCM_LIB_DIR}/gdcmCommon.lib
        ${GDCM_LIB_DIR}/gdcmDSED.lib
)
find_package(GDCM COMPONENTS Dict REQUIRED)

message(STATUS "GDCM_DIR:${GDCM_DIR}")
message(STATUS "GDCM_INCLUDE_DIRS:${GDCM_INCLUDE_DIRS}")
message(STATUS "Qt6_LIBS:${Qt6_LIBS}")


set(SOURCE_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sources)

aux_source_directory(src SOURCES_SRC)
set(SOURCE_DIR ${SOURCES_SRC})

include_directories(
        ${OpenCV_INCLUDE_DIRS}
        ${GDCM_INCLUDE_DIRS}
        ${SOURCE_INCLUDE_DIR}
)

add_executable(untitled2 main.cpp ${SOURCE_DIR})
target_link_libraries(untitled2
        Qt::Core
        Qt::Gui
        Qt::Widgets
        Qt::OpenGL
        Qt6::OpenGLWidgets
        ${OpenCV_LIBS}
        ${GDCM_LIBS}
)

if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()

相關文章