cmake_minimum_required(VERSION 3.5) project(test VERSION 0.1 LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) ## qt auto call uic, moc, rcc compilers set(CMAKE_AUTOUIC ON) # auto call uic compiler to process qt ui files. set(CMAKE_AUTOMOC ON) # auto call moc compiler to process qt signal-slot source files. set(CMAKE_AUTORCC ON) # auto call rcc complier to process qt resource files. ## find required libraries # qt libs find_package(Qt5 COMPONENTS Widgets REQUIRED) # boost find_package(Boost 1.65 REQUIRED COMPONENTS context thread) # protobuf find_package(Protobuf REQUIRED) ## common include directories include_directories( "${Protobuf_INCLUDE_DIR}" "${Boost_INCLUDE_DIR}" ) ## common link directories link_directories( # "/usr/local/lib" ) ## header files set(PROJECT_HEADERS MainWindow.h ) ## source files set(PROJECT_SOURCES main.cpp MainWindow.cpp ) ## qt ui files set(PROJECT_QUIS MainWindow.ui ) ## qt resource files set(PROJECT_QRCS icons.qrc ) ## build target add_executable(test ${PROJECT_SOURCES} ${PROJECT_HEADERS} ${PROJECT_QUIS} ${PROJECT_QRCS} ) ## link required libraries target_link_libraries(test PRIVATE Qt5::Widgets ${Boost_LIBRARIES} ${Protobuf_LIBRARIES} )
基本模板如上,多个子项目的目录可以通过add_subdirectory添加即可,注意:
1.如果想在Qt Creator中实现快捷键在头文件\源文件\ui文件之间跳转最好将它们放在同一个目录;
2.Qt资源文件如图标这种需要放在单独的文件夹中,qrc文件本质就是个xml文件描述了资源文件的位置和分组;
3.ui和qrc文件需要跟源文件一起作为编译目标的"原材料"
标签:files,set,CMake,qt,##,PROJECT,Qt5,CMAKE,模板 From: https://www.cnblogs.com/djh5520/p/16918352.html