前置环境:Clion2024.1.2 cmake version: 3.2.8

cmake_minimum_required(VERSION 3.28)

https://cmake.org/cmake/help/latest/command/cmake_minimum_required.html

设置cmake最小版本

project(qtest08 VERSION 1.0.0 LANGUAGES CXX)

文档里的表述:project(<PROJECT-NAME> [<language-name>...]) project(<PROJECT-NAME> [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]] [DESCRIPTION <project-description-string>] [HOMEPAGE_URL <url-string>] [LANGUAGES <language-name>...])

设置项目名称,版本。。。。。

set(CMAKE_CXX_STANDARD 26)

这里CMAKE_CXX_STANDARDcmake-variables(7);指定cpp版本

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_PREFIX_PATH "D:/Qt/6.6.1/mingw_64/lib/cmake")

文档解释:Semicolon-separated list of directories specifying installation prefixes to be searched by the find_package(), find_program(), find_library(), find_file(), and find_path() commands. Each command will add appropriate subdirectories (like bin, lib, or include) as specified in its own documentation.

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS
#        Core
#        Gui
        Widgets
        REQUIRED)

根据文档,这是Config mode,In this mode, CMake searches for a file called <lowercasePackageName>-config.cmake or <PackageName>Config.cmake. It will also look for <lowercasePackageName>-config-version.cmake or <PackageName>ConfigVersion.cmake"D:\Qt\6.6.1\mingw_64\lib\cmake\Qt6\Qt6Config.cmake"

在Qt6Config.cmake中,

    # For examples using `find_package(...)` inside their CMakeLists.txt files:
    # Make CMake's AUTOGEN detect this Qt version properly
    set_directory_properties(PROPERTIES
                             QT_VERSION_MAJOR 6
                             QT_VERSION_MINOR 6
                             QT_VERSION_PATCH 1)

指明${QT_VERSION_MAJOR}的值

同样A package-specific list of required components may be listed after the COMPONENTS keyword. If any of these components are not able to be satisfied, the package overall is considered to be not found. If the REQUIRED option is also present, this is treated as a fatal error, otherwise execution still continues. As a form of shorthand, if the REQUIRED option is present, the COMPONENTS keyword can be omitted and the required components can be listed directly after REQUIRED.

也就是说,这两条可以用下面这一条替代

find_package(Qt6 COMPONENTS
        #        Core
        #        Gui
        Widgets
        REQUIRED)
qt_standard_project_setup()

Qt文档说明:Setup project-wide defaults to a standard arrangement. If versionless commands are disabled, use qt6_standard_project_setup() instead. It supports the same set of arguments as this command.

接下来建立目录./src与新的CmakeList.txt

set(PROJECT_SOURCES
        main.cpp
)

qt_add_executable(test8
        #        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
)

Qt文档解释:Creates and finalizes an application target of a platform-specific type.

target_link_libraries(test8 PRIVATE
        #        Qt${QT_VERSION_MAJOR}::Core
        #        Qt${QT_VERSION_MAJOR}::Gui
        Qt${QT_VERSION_MAJOR}::Widgets
)

文档解释:Specify libraries or flags to use when linking a given target and/or its dependents. Usage requirements from linked library targets will be propagated. Usage requirements of a target's dependencies affect compilation of its own sources.

The PUBLIC, PRIVATE and INTERFACE scope keywords can be used to specify both the link dependencies and the link interface in one command.

Libraries and targets following PUBLIC are linked to, and are made part of the link interface. Libraries and targets following PRIVATE are linked to, but are not made part of the link interface. Libraries following INTERFACE are appended to the link interface and are not used for linking <target>.(看不懂)

set_target_properties(test8 PROPERTIES
        WIN32_EXECUTABLE ON
        MACOSX_BUNDLE ON
)

文档解释:Build an executable with a WinMain entry point on windows.

When this property is set to true the executable when linked on Windows will be created with a WinMain() entry point instead of just main(). This makes it a GUI executable instead of a console application. See the CMAKE_MFC_FLAG variable documentation to configure use of the Microsoft Foundation Classes (MFC) for WinMain executables. This property is initialized by the value of the CMAKE_WIN32_EXECUTABLE variable if it is set when a target is created.

add_subdirectory(src)
add_subdirectory(resources)

最后在主CmakeList.txt中添加

./CmakeList.txt

cmake_minimum_required(VERSION 3.27)

project(test8   VERSION 1.0.0 #[[LANGUAGES cxx]])


set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_PREFIX_PATH "D:/Qt/6.6.1/mingw_64/lib/cmake")
#[[set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)]]
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS
#        Core
#        Gui
        Widgets
        REQUIRED)

#[[set(PROJECT_SOURCES
        src/main.cpp
)]]

qt_standard_project_setup()

#[[qt_add_executable(test8
#        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
)]]
#add_executable(test8 WIN32 main.cpp)

#[[target_link_libraries(test8 PRIVATE
#        Qt${QT_VERSION_MAJOR}::Core
#        Qt${QT_VERSION_MAJOR}::Gui
        Qt${QT_VERSION_MAJOR}::Widgets
)]]

add_subdirectory(src)
add_subdirectory(resources)
#[[set_target_properties(test8 PROPERTIES
        WIN32_EXECUTABLE ON
        MACOSX_BUNDLE ON
)]]




#add_subdirectory(resources)
#[[qt_add_resources(test8 imageresources
        PREFIX "D:/code/test8/resources"
        #        ${IMAGS}
        FILES ico.ico
)]]





#[[# 需要生成嵌入的文件
file(GLOB RELATIVE EMBED_FILES
        "${CMAKE_CURRENT_SOURCE_DIR}/resources/*.ico"
)
# 输出文件目录
set(GEN_EMBED_OUTPUT_ICON_DIR
        "${CMAKE_CURRENT_BINARY_DIR}/gen_icon"
)

file(MAKE_DIRECTORY ${GEN_EMBED_OUTPUT_ICON_DIR})
# 依次处理文件
foreach(input_src ${EMBED_FILES})
    get_filename_component(embed_filename ${input_src} NAME)
    set(gen_embed_file "${GEN_EMBED_OUTPUT_ICON_DIR}/${embed_filename}.ico")

    # Copy the file to the output directory
    configure_file(${input_src} ${gen_embed_file} COPYONLY)

    # Add the generated file to the target_sources
    target_sources(test8 PUBLIC ${gen_embed_file})
endforeach()]]

./src/CmakeList.txt

#set(ico "../resources/ico.ico")

set(PROJECT_SOURCES
        main.cpp
)

qt_add_executable(test8
        #        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
)

target_link_libraries(test8 PRIVATE
        #        Qt${QT_VERSION_MAJOR}::Core
        #        Qt${QT_VERSION_MAJOR}::Gui
        Qt${QT_VERSION_MAJOR}::Widgets
)

set_target_properties(test8 PROPERTIES
        WIN32_EXECUTABLE ON
        MACOSX_BUNDLE ON
)




#[[
qt_add_translations(test8
        PREFIX "../resources"
        TS_FILES test8_en.ts
)????????]]
#    FILE()

./resources

set(SOURCES_FILES
        ico.ico
        ic.png
)

#[[set(GEN_EMBED_OUTPUT_ICON_DIR
        "${CMAKE_CURRENT_BINARY_DIR}/gen_icon"
)]]
#file(MAKE_DIRECTORY ${GEN_EMBED_OUTPUT_ICON_DIR})
foreach (file ${SOURCES_FILES})
    configure_file(${file} ${CMAKE_CURRENT_BINARY_DIR}/${file} COPYONLY)
endforeach ()

main.cpp:

#include <iostream>
#include <QWidget>
#include <QApplication>
#include <QIcon>
#include <QWindow>
#include <iostream>
#include <filesystem>
int main(int argc ,char *argv[]) {
//    std::cout << "Hello, World!" << std::endl;
    QApplication a{argc,argv};
/*    std::filesystem::path p{"/resources/ico.ico"};
    std::cout<<std::filesystem::exists(p);*/
    QWidget w;
//    w.setWindowIcon(QIcon{"resources/ico.ico"});
    w.show();
    return a.exec();
}

环境:PATH=D:\Qt\6.6.1\mingw_64\bin