CMake的子模块中头文件和cpp文件在不同目录

项目目录

project_root/
    CMakeLists.txt (主)
    include/
        module1/
            module1.h
        module2/
            module2.h
    src/
        main.cpp
        module1/
            CMakeLists.txt
            module1.cpp
        module2/
            CMakeLists.txt
            module2.cpp

主CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(CMakeDemo)

set(CMAKE_CXX_STANDARD 17)

# 添加子模块
add_subdirectory(src/module1)
add_subdirectory(src/module2)


# 引入头文件
include_directories(
        ${PROJECT_SOURCE_DIR}/include/module1
        ${PROJECT_SOURCE_DIR}/include/module2
)


# 编译成可执行文件
add_executable(CMakeDemo src/main.cpp)

# 链接module1和module2
target_link_libraries(CMakeDemo module1 module2)

模块1CMakeLists.txt

# 编译成静态库
add_library(module1 STATIC test01.cpp)

# 加入头文件
target_include_directories(module1 PUBLIC ${PROJECT_SOURCE_DIR}/include/module1)

模块2CMakeLists.txt

# 编译成静态库
add_library(module2 STATIC module2.cpp)

# 加入头文件
target_include_directories(module2 PUBLIC ${PROJECT_SOURCE_DIR}/include/module2)

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇