CMakeList.txt的写法
- 前言
- 一个最近写好的CMakeList.txt
- 对上边CMakeList.txt的说明
- 修改
前言
复习一下CMakeList.txt的写法,以下面的一个CMakeList.txt为例
一个最近写好的CMakeList.txt
cmake_minimum_required(VERSION 3.17)project(QtVTKProject)#======================= INCLUSION OF Qt =======================#
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_PREFIX_PATH $ENV{QTDIR})
find_package(OpenGL)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5OpenGL REQUIRED)
find_package(Qt5Xml REQUIRED)
find_package(Qt5Widgets REQUIRED)#======================= INCLUSION OF VTK ======================#
set(VTK_DIR $ENV{VTK_DIR})
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})#======================= INCLUSION OF Our Code ======================#
set(FORMS_DIR "${CMAKE_SOURCE_DIR}/forms")
set(INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include")
set(SOURCE_DIR "${CMAKE_SOURCE_DIR}/src")include_directories(${FORMS_DIR})
include_directories(${INCLUDE_DIR})
include_directories(${SOURCE_DIR})file(GLOB_RECURSE SOURCES"${FORMS_DIR}/*.ui""${INCLUDE_DIR}/*.h""${SOURCE_DIR}/*.cpp"
)# Add the forms directory to the AUTOUIC search paths
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_AUTOUIC_SEARCH_PATHS} ${FORMS_DIR})# Add the executable
if (WIN32) add_executable(QtVTKProject WIN32 ${SOURCES})
elseif(UNIX)add_executable(QtVTKProject ${SOURCES})
endif()# Add the target includes
target_include_directories(QtVTKProject PRIVATE ${FORMS_DIR})
target_include_directories(QtVTKProject PRIVATE ${INCLUDE_DIR})
target_include_directories(QtVTKProject PRIVATE ${SOURCE_DIR})#===================== LINKING LIBRARIES =======================#
target_link_libraries( QtVTKProject Qt5::OpenGL)
target_link_libraries( QtVTKProject Qt5::Xml)
target_link_libraries( QtVTKProject Qt5::Widgets)
target_link_libraries( QtVTKProject ${QT_LIBRARIES} ${OPENGL_LIBRARIES})
target_link_libraries( QtVTKProject ${VTK_LIBRARIES})
对上边CMakeList.txt的说明
cmake_minimum_required(VERSION 3.17): 指定项目所需的最低CMake版本。project(QtVTKProject): 指定项目名称为"QtVTKProject"。#======================= INCLUSION OF Qt =======================#set(CMAKE_INCLUDE_CURRENT_DIR ON): 设置CMake包含当前目录。set(CMAKE_AUTOMOC ON): 设置自动运行moc(Meta-Object Compiler)工具,用于处理Qt的元对象系统。set(CMAKE_AUTOUIC ON): 设置自动运行uic(User Interface Compiler)工具,用于处理Qt的UI文件。set(CMAKE_PREFIX_PATH $ENV{QTDIR}): 设置Qt的安装路径。find_package(OpenGL): 查找OpenGL库。find_package(Qt5Core REQUIRED): 查找并要求Qt5Core库。find_package(Qt5Gui REQUIRED): 查找并要求Qt5Gui库。find_package(Qt5OpenGL REQUIRED): 查找并要求Qt5OpenGL库。find_package(Qt5Xml REQUIRED): 查找并要求Qt5Xml库。find_package(Qt5Widgets REQUIRED): 查找并要求Qt5Widgets库。#======================= INCLUSION OF VTK ======================#set(VTK_DIR $ENV{VTK_DIR}): 设置VTK的安装路径。find_package(VTK REQUIRED): 查找并要求VTK库。include(${VTK_USE_FILE}): 包含VTK的使用文件。#======================= INCLUSION OF Our Code ======================#set(FORMS_DIR "${CMAKE_SOURCE_DIR}/forms"): 设置forms目录的路径。set(INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include"): 设置include目录的路径。set(SOURCE_DIR "${CMAKE_SOURCE_DIR}/src"): 设置src目录的路径。include_directories(${FORMS_DIR}): 包含forms目录以供编译器搜索ui文件。include_directories(${INCLUDE_DIR}): 包含include目录以供编译器搜索头文件。include_directories(${SOURCE_DIR}): 包含src目录以供编译器搜索源文件。file(GLOB_RECURSE SOURCES ...): 使用GLOB_RECURSE命令获取指定目录下的所有源文件。set(CMAKE_AUTOUIC_SEARCH_PATHS ...): 将forms目录添加到AUTOUIC搜索路径。if (WIN32) ... elseif(UNIX) ... endif(): 根据操作系统类型选择生成可执行文件的选项。
在Windows上使用WIN32选项,而在Unix上不包含此选项。add_executable(QtVTKProject ... ${SOURCES}): 添加可执行文件的构建目标,并指定源文件。target_include_directories(QtVTKProject PRIVATE ...): 为目标添加私有的包含目录。#===================== LINKING LIBRARIES =======================#target_link_libraries( QtVTKProject ...): 链接所需的库文件到目标。
最后,该CMakeLists.txt文件的主要目标是构建一个名为"QtVTKProject"的可执行文件。它依赖于Qt和VTK库,并包含了一些源文件和UI文件。通过设置相关的包含目录和链接库,确保了项目的正确构建和链接。
修改
如果只需要Qt库的话,可以简单修改一下:
cmake_minimum_required(VERSION 3.17)project(Demo1)#======================= INCLUSION OF Qt =======================#
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_PREFIX_PATH $ENV{QTDIR})
find_package(OpenGL)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5OpenGL REQUIRED)
find_package(Qt5Xml REQUIRED)
find_package(Qt5Widgets REQUIRED)#======================= INCLUSION OF Our Code ======================#
set(FORMS_DIR "${CMAKE_SOURCE_DIR}/forms")
set(INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include")
set(SOURCE_DIR "${CMAKE_SOURCE_DIR}/src")include_directories(${FORMS_DIR})
include_directories(${INCLUDE_DIR})
include_directories(${SOURCE_DIR})file(GLOB_RECURSE SOURCES"${FORMS_DIR}/*.ui""${INCLUDE_DIR}/*.h""${SOURCE_DIR}/*.cpp"
)# Add the forms directory to the AUTOUIC search paths
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_AUTOUIC_SEARCH_PATHS} ${FORMS_DIR})# Add the executable
if (WIN32) add_executable(Demo1 WIN32 ${SOURCES})
elseif(UNIX)add_executable(Demo1 ${SOURCES})
endif()# Add the target includes
target_include_directories(Demo1 PRIVATE ${FORMS_DIR})
target_include_directories(Demo1 PRIVATE ${INCLUDE_DIR})
target_include_directories(Demo1 PRIVATE ${SOURCE_DIR})#===================== LINKING LIBRARIES =======================#
target_link_libraries( Demo1 Qt5::OpenGL)
target_link_libraries( Demo1 Qt5::Xml)
target_link_libraries( Demo1 Qt5::Widgets)
target_link_libraries( Demo1 ${QT_LIBRARIES} ${OPENGL_LIBRARIES})