一个好的自行编程式的学习源:https://github.com/ttroy50/cmake-examples

把里面的

两个目录认真的学习一遍,最好自己能够动手跟着做一遍。

基础

当目录中存在CMakeLists.txt,使用cmake命令即可完成makefile的构建,后使用make命令即可执行构建任务

最小CMakeLists.txt中应该包含以下信息

Set the project name

project (hello_headers)

Add an executable

add_executable(hello_cmake main.cpp)

2全局变量

Variable Info
CMAKE_SOURCE_DIR The root source directory
CMAKE_CURRENT_SOURCE_DIR The current source directory if using sub-projects and directories.
PROJECT_SOURCE_DIR The source directory of the current cmake project.
CMAKE_BINARY_DIR The root binary / build directory. This is the directory where you ran the cmake command.
CMAKE_CURRENT_BINARY_DIR The build directory you are currently in.
PROJECT_BINARY_DIR The build directory for the current project.

设置include文件夹

target_include_directories(target

PRIVATE

${PROJECT_SOURCE_DIR}/include

)

CMakeLists.txt实例