构建可执行文件(编辑器)并将其链接到库的示例(突出显示)。项目结构简单明了,每个子项目都需要一个主CMakeLists和一个目录:
CMakeLists.txt editor/ CMakeLists.txt src/ editor.cpp highlight/ CMakeLists.txt include/ highlight.h src/ highlight.cpp
母版CMakeLists.txt包含全局定义,并add_subdirectory针对每个子项目进行调用:
cmake_minimum_required(VERSION 3.0) project(Example) add_subdirectory(highlight) add_subdirectory(editor)
库的CMakeLists.txt分配源并包括目录。通过使用target_include_directories()而不是include_directories()include,目录将传播到库用户:
cmake_minimum_required(VERSION 3.0) project(highlight) add_library(${PROJECT_NAME} src/highlight.cpp) target_include_directories(${PROJECT_NAME} PUBLIC include)
该应用程序的CMakeLists.txt分配源并链接突出显示库。指向hightlighter的二进制文件及其包含的路径由cmake自动处理:
cmake_minimum_required(VERSION 3.0) project(editor) add_executable(${PROJECT_NAME} src/editor.cpp) target_link_libraries(${PROJECT_NAME} PUBLIC highlight)