cmake 没有包的简单方法


构建可执行文件(编辑器)并将库(突出显示)链接到它的示例。项目结构很简单,它需要一个主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 dirs将传播给库用户:

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)