做网站 公司,噼里啪啦免费观看高清,网络规划设计师教程第二版电子版下载,搭建网站做淘宝客文章目录 1.概述2.添加子目录3.指定二进制目录4.排除子目录5.使用别名6.传递变量7.检查子目录是否存在 1.概述 add_subdirectory是 CMake 中的一个命令#xff0c;用于向当前项目添加一个子目录。它的语法如下#xff1a; #mermaid-svg-9zKJ3AvoVRln9hon {font-family:… 文章目录 1.概述2.添加子目录3.指定二进制目录4.排除子目录5.使用别名6.传递变量7.检查子目录是否存在 1.概述 add_subdirectory是 CMake 中的一个命令用于向当前项目添加一个子目录。它的语法如下 #mermaid-svg-9zKJ3AvoVRln9hon {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-9zKJ3AvoVRln9hon .error-icon{fill:#552222;}#mermaid-svg-9zKJ3AvoVRln9hon .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9zKJ3AvoVRln9hon .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-9zKJ3AvoVRln9hon .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9zKJ3AvoVRln9hon .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9zKJ3AvoVRln9hon .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9zKJ3AvoVRln9hon .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9zKJ3AvoVRln9hon .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9zKJ3AvoVRln9hon .marker.cross{stroke:#333333;}#mermaid-svg-9zKJ3AvoVRln9hon svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9zKJ3AvoVRln9hon .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-9zKJ3AvoVRln9hon .cluster-label text{fill:#333;}#mermaid-svg-9zKJ3AvoVRln9hon .cluster-label span{color:#333;}#mermaid-svg-9zKJ3AvoVRln9hon .label text,#mermaid-svg-9zKJ3AvoVRln9hon span{fill:#333;color:#333;}#mermaid-svg-9zKJ3AvoVRln9hon .node rect,#mermaid-svg-9zKJ3AvoVRln9hon .node circle,#mermaid-svg-9zKJ3AvoVRln9hon .node ellipse,#mermaid-svg-9zKJ3AvoVRln9hon .node polygon,#mermaid-svg-9zKJ3AvoVRln9hon .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-9zKJ3AvoVRln9hon .node .label{text-align:center;}#mermaid-svg-9zKJ3AvoVRln9hon .node.clickable{cursor:pointer;}#mermaid-svg-9zKJ3AvoVRln9hon .arrowheadPath{fill:#333333;}#mermaid-svg-9zKJ3AvoVRln9hon .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-9zKJ3AvoVRln9hon .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-9zKJ3AvoVRln9hon .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-9zKJ3AvoVRln9hon .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-9zKJ3AvoVRln9hon .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-9zKJ3AvoVRln9hon .cluster text{fill:#333;}#mermaid-svg-9zKJ3AvoVRln9hon .cluster span{color:#333;}#mermaid-svg-9zKJ3AvoVRln9hon div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-9zKJ3AvoVRln9hon :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}#mermaid-svg-9zKJ3AvoVRln9hon .default*{fill:#f9f!important;stroke:#333!important;stroke-width:1px!important;}#mermaid-svg-9zKJ3AvoVRln9hon .default span{fill:#f9f!important;stroke:#333!important;stroke-width:1px!important;}#mermaid-svg-9zKJ3AvoVRln9hon .special*{fill:#9f9!important;stroke:#333!important;stroke-width:1px!important;}#mermaid-svg-9zKJ3AvoVRln9hon .special span{fill:#9f9!important;stroke:#333!important;stroke-width:1px!important;} add_subdirectory 添加子目录 指定二进制目录 排除子目录 使用别名 传递变量 检查子目录是否存在 将子项目目录添加到构建中 为子项目指定不同的构建输出目录 在特定条件下排除子目录 为子项目或库创建别名 向子目录传递CMake变量 在添加子目录前验证其存在 add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])其中
source_dir 是要添加的子目录的路径。binary_dir 是可选的二进制目录路径用于指定子目录的构建输出目录。如果未指定将使用默认的构建输出目录。EXCLUDE_FROM_ALL 是可选的参数用于指定是否将子目录排除在构建过程之外。如果指定了该参数子目录将不会在构建时被构建。
使用 add_subdirectory 命令后CMake 会在指定的 source_dir 中查找 CMakeLists.txt 文件并执行该文件中的命令。这样您可以将子目录作为独立的项目进行构建并与主项目进行协作。
通常在子目录的 CMakeLists.txt 文件中您可以定义和配置子目录的构建过程、生成库或可执行文件并使用 target_link_libraries 命令将子目录的目标与主项目的目标进行链接。
2.添加子目录
假设您的项目结构如下
project/
├── CMakeLists.txt
├── main.cpp
└── subdirectory/├── CMakeLists.txt├── lib/│ ├── CMakeLists.txt│ ├── lib.cpp│ └── lib.h└── tests/├── CMakeLists.txt└── test.cpp在主项目的 CMakeLists.txt 文件中您可以使用 add_subdirectory 命令将子目录添加到主项目中
cmake_minimum_required(VERSION 3.12)
project(MyProject)# 主项目的构建配置# 添加子目录
add_subdirectory(subdirectory)# 主项目的目标和链接等配置在子目录的 CMakeLists.txt 文件中您可以定义子目录的构建过程和目标
# 子目录的 CMakeLists.txt# 子目录的构建配置# 生成库或可执行文件
add_library(mylib lib/lib.cpp lib/lib.h)# 子目录的目标和链接等配置通过 add_subdirectory 命令子目录中的 CMakeLists.txt 文件将被执行子目录的构建过程和目标将与主项目一起构建和链接。
请注意add_subdirectory 命令应该在主项目的 CMakeLists.txt 文件中使用并且应该在主项目的目标和链接配置之前。这样可以确保子目录的目标在主项目的构建过程中可用。
3.指定二进制目录 可以通过在 add_subdirectory 命令中提供可选的 binary_dir 参数来指定子目录的二进制目录。这样可以将子目录的构建输出与主项目的构建输出分开使项目结构更清晰。例如 add_subdirectory(subdirectory bin)这将在主项目的构建目录下创建一个名为 bin 的子目录用于存放子目录的构建输出。
4.排除子目录 如果您希望在构建主项目时排除某个子目录可以使用 EXCLUDE_FROM_ALL 参数。这在某些情况下很有用比如只构建主项目的特定部分或避免构建测试目录。例如 add_subdirectory(tests EXCLUDE_FROM_ALL)这将排除名为 tests 的子目录使其不会在构建主项目时被构建。
5.使用别名 在主项目中您可以为子目录的目标创建一个别名以便在主项目中更方便地引用子目录的目标。这在链接库或设置依赖关系时很有用。例如 add_subdirectory(subdirectory)
add_executable(myapp main.cpp)
target_link_libraries(myapp PRIVATE subdirectory::mylib)这里subdirectory::mylib 是对子目录中名为 mylib 的目标的别名可以直接在主项目中使用。
6.传递变量 使用 add_subdirectory 命令可以将变量传递给子目录的 CMakeLists.txt 文件。您可以在主项目的 CMakeLists.txt 文件中定义变量并在子目录的 CMakeLists.txt 文件中使用。例如 set(MY_VARIABLE Hello, World!)
add_subdirectory(subdirectory)在子目录的 CMakeLists.txt 文件中可以访问和使用 MY_VARIABLE 变量。
7.检查子目录是否存在 在使用 add_subdirectory 命令之前可以使用 if(EXISTS ...) 检查子目录是否存在。这可以避免在子目录不存在时引发错误。例如 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/subdirectory)add_subdirectory(subdirectory)
else()message(FATAL_ERROR Subdirectory subdirectory does not exist.)
endif()这将在子目录存在时添加子目录否则会发出错误消息并停止构建。