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)