北京网站建设 案例,实战网站开发,中国域名后缀是什么,营销型企业网站建设包括什么#x1f608;「CSDN主页」#xff1a;传送门 #x1f608;「Bilibil首页」#xff1a;传送门 #x1f608;「本专栏容」#xff1a;CMake入门教程 #x1f608;「动动你的小手」#xff1a;点赞#x1f44d;收藏⭐️评论#x1f4dd; 文章目录 1. 基本用法2. 添加单个… 「CSDN主页」传送门 「Bilibil首页」传送门 「本专栏容」CMake入门教程 「动动你的小手」点赞收藏⭐️评论 文章目录 1. 基本用法2. 添加单个包含目录3. 添加多个包含目录4. 控制包含目录的搜索顺序5. 区分系统头文件和用户头文件6.使用技巧6.1 避免全局设置6.2 使用target_include_directories6.3 区分私有和公共包含目录6.4 管理第三方库的包含目录 CMake的
include_directories命令是用于指定头文件搜索路径的重要工具。在编译C代码时正确设置包含目录对于确保编译器能够找到所有必要的头文件至关重要。 #mermaid-svg-5QfPFy06lIOG4syV {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-5QfPFy06lIOG4syV .error-icon{fill:#552222;}#mermaid-svg-5QfPFy06lIOG4syV .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-5QfPFy06lIOG4syV .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-5QfPFy06lIOG4syV .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-5QfPFy06lIOG4syV .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-5QfPFy06lIOG4syV .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-5QfPFy06lIOG4syV .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-5QfPFy06lIOG4syV .marker{fill:#333333;stroke:#333333;}#mermaid-svg-5QfPFy06lIOG4syV .marker.cross{stroke:#333333;}#mermaid-svg-5QfPFy06lIOG4syV svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-5QfPFy06lIOG4syV .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-5QfPFy06lIOG4syV .cluster-label text{fill:#333;}#mermaid-svg-5QfPFy06lIOG4syV .cluster-label span{color:#333;}#mermaid-svg-5QfPFy06lIOG4syV .label text,#mermaid-svg-5QfPFy06lIOG4syV span{fill:#333;color:#333;}#mermaid-svg-5QfPFy06lIOG4syV .node rect,#mermaid-svg-5QfPFy06lIOG4syV .node circle,#mermaid-svg-5QfPFy06lIOG4syV .node ellipse,#mermaid-svg-5QfPFy06lIOG4syV .node polygon,#mermaid-svg-5QfPFy06lIOG4syV .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-5QfPFy06lIOG4syV .node .label{text-align:center;}#mermaid-svg-5QfPFy06lIOG4syV .node.clickable{cursor:pointer;}#mermaid-svg-5QfPFy06lIOG4syV .arrowheadPath{fill:#333333;}#mermaid-svg-5QfPFy06lIOG4syV .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-5QfPFy06lIOG4syV .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-5QfPFy06lIOG4syV .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-5QfPFy06lIOG4syV .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-5QfPFy06lIOG4syV .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-5QfPFy06lIOG4syV .cluster text{fill:#333;}#mermaid-svg-5QfPFy06lIOG4syV .cluster span{color:#333;}#mermaid-svg-5QfPFy06lIOG4syV 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-5QfPFy06lIOG4syV :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}#mermaid-svg-5QfPFy06lIOG4syV .default*{fill:#f9f!important;stroke:#333!important;stroke-width:1px!important;}#mermaid-svg-5QfPFy06lIOG4syV .default span{fill:#f9f!important;stroke:#333!important;stroke-width:1px!important;}#mermaid-svg-5QfPFy06lIOG4syV .special*{fill:#9f9!important;stroke:#333!important;stroke-width:1px!important;}#mermaid-svg-5QfPFy06lIOG4syV .special span{fill:#9f9!important;stroke:#333!important;stroke-width:1px!important;} include_directories 使用 添加单个包含目录 添加多个包含目录 控制搜索顺序 区分系统和用户头文件 使用技巧 include_directories(include) include_directories(include1 include2) include_directories(AFTER include1 BEFORE include2) include_directories(SYSTEM system_include) 避免全局设置 使用target_include_directories target_include_directories(my_target PRIVATE include) 区分私有和公共包含目录 target_include_directories(my_library PUBLIC include_public) target_include_directories(my_library PRIVATE include_private) 管理第三方库包含目录 find_package(Boost REQUIRED) target_include_directories(my_target PRIVATE \${Boost_INCLUDE_DIRS}) 1. 基本用法
include_directories命令用于添加包含目录其基本语法如下
include_directories([AFTER|BEFORE] [SYSTEM] directory1 [directory2 ...])directory1, directory2, … 是要添加的包含目录。
2. 添加单个包含目录
include_directories(include)这会将项目目录下的include目录添加到包含路径中。
3. 添加多个包含目录
include_directories(include1 include2)这会同时将include1和include2目录添加到包含路径中。
4. 控制包含目录的搜索顺序
include_directories(AFTER include1 BEFORE include2)使用AFTER和BEFORE关键字可以控制包含目录的搜索顺序。在此例中include1会在include2之后被搜索。
5. 区分系统头文件和用户头文件
include_directories(SYSTEM system_include)使用SYSTEM参数可以将特定目录标记为系统头文件目录这有助于减少编译器对于系统头文件的警告。
6.使用技巧
6.1 避免全局设置
尽可能避免在项目的根CMakeLists.txt中全局设置包含目录以防止不同目标间的潜在冲突。
6.2 使用target_include_directories
在现代CMake中推荐使用target_include_directories而不是include_directories
target_include_directories(my_target PRIVATE include)这将include目录添加到特定目标my_target的私有包含路径。
6.3 区分私有和公共包含目录
对于库目标使用PRIVATE和PUBLIC关键字区分私有和公共包含目录
target_include_directories(my_library PUBLIC include_public)
target_include_directories(my_library PRIVATE include_private)PUBLIC包含目录会被库的使用者继承而PRIVATE包含目录仅用于库本身的构建。
6.4 管理第三方库的包含目录
使用find_package或pkg_check_modules来管理第三方库并使用其提供的包含目录
find_package(Boost REQUIRED)
target_include_directories(my_target PRIVATE ${Boost_INCLUDE_DIRS})这样可以确保第三方库的包含目录被正确添加。
通过这些方法您可以有效地管理和优化CMake项目中的包含目录确保构建系统的可维护性和可移植性。