CMake编译目录包含命令include_directories简介

本文使用的相关软件环境信息如下:

软件名称 软件版本
Linux操作系统 Ubuntu 22.04 LTS(X64)
cmake 3.22.1
  • include_directories

    命令格式:

    include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])

    该命令主要是将目录添加到编译器的头文件的搜索目录之下,具体介绍见这里。执行该命令后,会将指定的目录添加到当前CMakeLists.txt文件的INCLUDE_DIRECTORIES目录属性和INCLUDE_DIRECTORIES目标文件属性中。接下来看一个实例,看include_directories添加目录之后,INCLUDE_DIRECTORIES属性内容的变化。目录结构如下,后续没有特殊说明,执行命令的目录为include_directories:

    include_directories/
    ├── CMakeLists.txt
    ├── main.cpp
    ├── testdir1
    │   ├── CMakeLists.txt
    │   ├── test1.cpp
    │   └── test1.h
    │   └── testdir1_subdir
    └── testdir2
        ├── CMakeLists.txt
        ├── test2.cpp
        └── test2.hs
    

    include_directories/CMakeLists.txt内容如下:

    cmake_minimum_required(VERSION 3.22)
    project(dir_test)
    
    include_directories(testdir1)
    message("--$ include_directories: testdir1")
    
    
    # 获取当前文件的INCLUDE_DIRECTORIES目录属性
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("--$ in directory: ${CMAKE_CURRENT_SOURCE_DIR}, include dir list: ${dirs}")
    
    # 编译testdir1下的库
    add_subdirectory(testdir1)
    message("--$ after compile lib test1")
    get_property(dirs TARGET test1 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test1 include dir list: ${dirs}")
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("--$ current file include dir list: ${dirs}")
    
    
    # 编译testdir2下的库
    add_subdirectory(testdir2)
    message("--$ after compile lib test2")
    get_property(dirs TARGET test1 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test1 include dir list: ${dirs}")
    get_property(dirs TARGET test2 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test2 include dir list: ${dirs}")
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("--$ current file include dir list: ${dirs}")
    
    
    add_executable(main main.cpp)
    message("--$ after add exe main")
    get_property(dirs TARGET test1 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test1 include dir list: ${dirs}")
    get_property(dirs TARGET test2 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test2 include dir list: ${dirs}")
    get_property(dirs TARGET main PROPERTY INCLUDE_DIRECTORIES)
    message("--$ executable main include dir list: ${dirs}")
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("--$ current file include dir list: ${dirs}")
    message("---")
    
    
    target_link_libraries(main test1)
    message("--$ after add lib test1")
    get_property(dirs TARGET test1 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test1 include dir list: ${dirs}")
    get_property(dirs TARGET test2 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test2 include dir list: ${dirs}")
    get_property(dirs TARGET main PROPERTY INCLUDE_DIRECTORIES)
    message("--$ executable main include dir list: ${dirs}")
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("--$ current file include dir list: ${dirs}")
    message("---")
    
    
    target_link_libraries(main test2)
    message("--$ after add lib test2")
    get_property(dirs TARGET test1 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test1 include dir list: ${dirs}")
    get_property(dirs TARGET test2 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test2 include dir list: ${dirs}")
    get_property(dirs TARGET main PROPERTY INCLUDE_DIRECTORIES)
    message("--$ executable main include dir list: ${dirs}")
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("--$ current file include dir list: ${dirs}")
    message("---")
    
    
    include_directories(testdir2)
    message("--$ include_directories: testdir2")
    get_property(dirs TARGET test1 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test1 include dir list: ${dirs}")
    get_property(dirs TARGET test2 PROPERTY INCLUDE_DIRECTORIES)
    message("--$ lib test2 include dir list: ${dirs}")
    get_property(dirs TARGET main PROPERTY INCLUDE_DIRECTORIES)
    message("--$ executable main include dir list: ${dirs}")
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("--$ current file include dir list: ${dirs}")
    message("---")
    

    include_directories/main.cpp内容:

    #include "test1.h"
    #include "test2.h"
    
    int main(int argc, char** argv)
    {
        test1_print();
        test2_print();
    }
    

    include_directories/testdir1/CMakeLists.txt内容如下:

    message("---")
    message("---Enter directory: ${CMAKE_CURRENT_SOURCE_DIR}")
    
    include_directories(testdir1_subdir)
    
    # 获取当前文件的INCLUDE_DIRECTORIES目录属性
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("   --$ include dir list: ${dirs}")
    
    add_library(test1 test1.cpp)
    message("   --$ after add lib test1")
    get_property(dirs TARGET test1 PROPERTY INCLUDE_DIRECTORIES)
    message("   --$ lib test1 include dir list: ${dirs}")
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("   --$ current file include dir list: ${dirs}")
    
    message("---Exist directory: ${CMAKE_CURRENT_SOURCE_DIR}")
    message("---")
    
    

    include_directories/testdir1/test1.cpp和include_directories/testdir1/test1.h文件内容如下:

    // test1.h
    #ifndef __TEST1_T__
    #define __TEST1_T__
    
    void test1_print();
    
    #endif
    
    // test1.cpp
    #include "test1.h"
    #include <iostream>
    
    void test1_print()
    {
        std::cout << "From test1: hello!" << std::endl;
    }
    
    

    include_directories/testdir2/CMakeLists.txt内容如下:

    message("---")
    message("---Enter directory: ${CMAKE_CURRENT_SOURCE_DIR}")
    
    # 获取当前文件的INCLUDE_DIRECTORIES目录属性
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("   --$ include dir list: ${dirs}")
    
    add_library(test2 test2.cpp)
    message("   --$ after add lib test2")
    get_property(dirs TARGET test2 PROPERTY INCLUDE_DIRECTORIES)
    message("   --$ lib test2 include dir list: ${dirs}")
    get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
    message("   --$ current file include dir list: ${dirs}")
    
    message("---Exist directory: ${CMAKE_CURRENT_SOURCE_DIR}")
    message("---")
    

    include_directories/testdir2/test2.cpp和include_directories/testdir2/test2.h文件内容如下:

    // test2.h
    #ifndef __TEST2_T__
    #define __TEST2_T__
    
    void test2_print();
    
    #endif
    
    // test2.cpp
    #include "test2.h"
    #include <iostream>
    
    void test2_print()
    {
        std::cout << "From test2: hello!" << std::endl;
    }
    

    运行cmake .make以及./main等命令,关键输出如下:

    $cmake .
    ……
    --$ include_directories: testdir1
    --$ in directory: /XXX/include_directories, include dir list: /XXX/include_directories/testdir1
    ---
    ---Enter directory: /XXX/include_directories/testdir1
       --$ include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir1/testdir1_subdir
       --$ after add lib test1
       --$ lib test1 include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir1/testdir1_subdir
       --$ current file include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir1/testdir1_subdir
    ---Exist directory: /XXX/include_directories/testdir1
    ---
    --$ after compile lib test1
    --$ lib test1 include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir1/testdir1_subdir
    --$ current file include dir list: /XXX/include_directories/testdir1
    ---
    ---Enter directory: /XXX/include_directories/testdir2
       --$ include dir list: /XXX/include_directories/testdir1
       --$ after add lib test2
       --$ lib test2 include dir list: /XXX/include_directories/testdir1
       --$ current file include dir list: /XXX/include_directories/testdir1
    ---Exist directory: /XXX/include_directories/testdir2
    ---
    --$ after compile lib test2
    --$ lib test1 include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir1/testdir1_subdir
    --$ lib test2 include dir list: /XXX/include_directories/testdir1
    --$ current file include dir list: /XXX/include_directories/testdir1
    --$ after add exe main
    --$ lib test1 include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir1/testdir1_subdir
    --$ lib test2 include dir list: /XXX/include_directories/testdir1
    --$ executable main include dir list: /XXX/include_directories/testdir1
    --$ current file include dir list: /XXX/include_directories/testdir1
    ---
    --$ after add lib test1
    --$ lib test1 include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir1/testdir1_subdir
    --$ lib test2 include dir list: /XXX/include_directories/testdir1
    --$ executable main include dir list: /XXX/include_directories/testdir1
    --$ current file include dir list: /XXX/include_directories/testdir1
    ---
    --$ after add lib test2
    --$ lib test1 include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir1/testdir1_subdir
    --$ lib test2 include dir list: /XXX/include_directories/testdir1
    --$ executable main include dir list: /XXX/include_directories/testdir1
    --$ current file include dir list: /XXX/include_directories/testdir1
    ---
    --$ include_directories: testdir2
    --$ lib test1 include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir1/testdir1_subdir
    --$ lib test2 include dir list: /XXX/include_directories/testdir1
    --$ executable main include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir2
    --$ current file include dir list: /XXX/include_directories/testdir1;/XXX/include_directories/testdir2
    ---
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /XXX/include_directories
    
    $ make
    Consolidate compiler generated dependencies of target test2
    [ 16%] Building CXX object testdir2/CMakeFiles/test2.dir/test2.cpp.o
    [ 33%] Linking CXX static library libtest2.a
    [ 33%] Built target test2
    Consolidate compiler generated dependencies of target test1
    [ 66%] Built target test1
    Consolidate compiler generated dependencies of target main
    [ 83%] Building CXX object CMakeFiles/main.dir/main.cpp.o
    [100%] Linking CXX executable main
    [100%] Built target main
    
    $ ./main 
    From test1: hello!
    From test2: hello!
    

    以上结果表明了几点:

    1、include_directories添加的目录,会自动添加到当前所在的CMakeLists.txt文件的INCLUDE_DIRECTORIES目录属性,以及在当前CMakeLists.txt文件中定义的目标(通过add_executable/add_library等添加的目标)属性INCLUDE_DIRECTORIES中。

    2、include_directories添加的目录,会传递到当前所在CMakeLists.txt文件中通过add_subdirectory增加的下层CMakeLists.txt中(add_subdirectory语句之后调用的include_directories添加的目录无法传递)。

    3、下层CMakeLists.txt文件中调用的include_directories添加的目录,不会传递到调用它的上层CMakeLists.txt文件。

    4、上层CMakeLists.txt中的include_directories与add_subdirectory相对位置对子CMakeLists.txt中能看到的目录有影响。

    对我们的例子来说:

    1、可执行文件main能看到目录:testdir1、testdir2

    2、库文件test1能看到目录:testdir1、testdir1_subdir,无法看到testdir2,是因为调用add_subdirectory添加子目录testdir1时,还未通过include_directories包含testdir2

    3、库文件test2能看到目录:testdir1,没错!这意味对于本文的例子来说,test2.cpp可以直接包含test1.h文件并调用test1_print()接口

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342

推荐阅读更多精彩内容