Cmake命令之add_subdirectory介绍

摘要:
命令格式add_subdirectory添加一个子目录并构建该子目录。命令解析source_dir必选参数。场景3:父目录CMakeLists.txt的add_subdirectory指定了EXCLUDE_FROM_ALL选项。#父目录下的CMakeLists.txtcmake_minimum_requiredprojectadd_subdirectoryadd_executable在父目录下调用cmake.构建之后,在output目录或sub目录下不会出现libsub.a库,说明当指定EXCLUDE_FROM_ALL选项,子目录的目标文件不会生成。最后,以一个完整的例子来结束本文,父目录下的main.cpp和CMakeList.txt如下:#父目录下的CMakeLists.txtcmake_minimum_requiredprojectinclude_directoriesadd_subdirectoryadd_executabletarget_link_libraries#父目录下的main.cpp#include"test.h"#includeintmain{std::cout˂˂"Inmain..."˂˂std::endl;test("hello,world!
  • 命令格式

    add_subdirectory (source_dir [binary_dir] [EXCLUDE_FROM_ALL])
    添加一个子目录并构建该子目录。

  • 命令解析

    • source_dir
      必选参数。该参数指定一个子目录,子目录下应该包含CMakeLists.txt文件和代码文件。子目录可以是相对路径也可以是绝对路径,如果是相对路径,则是相对当前目录的一个相对路径。
    • binary_dir
      可选参数。该参数指定一个目录,用于存放输出文件。可以是相对路径也可以是绝对路径,如果是相对路径,则是相对当前输出目录的一个相对路径。如果该参数没有指定,则默认的输出目录使用source_dir
    • EXCLUDE_FROM_ALL
      可选参数。当指定了该参数,则子目录下的目标不会被父目录下的目标文件包含进去,父目录的CMakeLists.txt不会构建子目录的目标文件,必须在子目录下显式去构建。例外情况:当父目录的目标依赖于子目录的目标,则子目录的目标仍然会被构建出来以满足依赖关系(例如使用了target_link_libraries)
  • 举例说明

    目录结构及说明如下:

    ├── CMakeLists.txt    #父目录的CMakeList.txt
    ├── main.cpp    #源文件,包含main函数
    ├── sub    #子目录
     └── CMakeLists.txt    #子目录的CMakeLists.txt
     └── test.h    #子目录头文件
     └── test.cpp    #子目录源文件

    子目录sub 下的test.cpp定义了一个函数test(),将输入参数打印出来,相应的头文件test.h则对test()进行声明,CMakelists.txt则将sub下的源文件编译成库文件。

    //  sub/test.cpp  
    #include "test.h"
    #include <iostream>
    
    void test(std::string str)
    {
        std::cout << str << std::endl;
    }
    
    //  sub/test.h
    #include <string>
    
    void test(std::string str);
    
    # sub/CMakeLists.txt
    cmake_minimum_required(VERSION 3.10.2)
    project(sub)
    add_library(sub test.cpp)
    
    • 场景1:父目录CMakeLists.txtadd_subdirectory 只指定了source_dir
    # 父目录下的CMakeLists.txt
    cmake_minimum_required(VERSION 3.10.2)
    project(test)
    
    add_subdirectory(sub) 
    

    在父目录下调用cmake .构建之后,在sub目录下会出现libsub.a库,说明当不指定binary_dir,输出目标文件就会放到source_dir目录下。

    • 场景2:父目录CMakeLists.txtadd_subdirectory 指定了source_dirbinary_dir
    # 父目录下的CMakeLists.txt
    cmake_minimum_required(VERSION 3.10.2)
    project(test)
    
    add_subdirectory(sub output) 
    

    在父目录下调用cmake .构建之后,在output目录下会出现libsub.a库,sub目录下则没有libsub.a。说明当指定binary_dir,输出目标文件就会放到binary_dir目录下。

    • 场景3:父目录CMakeLists.txtadd_subdirectory 指定了EXCLUDE_FROM_ALL选项。
    # 父目录下的CMakeLists.txt
    cmake_minimum_required(VERSION 3.10.2)
    project(test)
    
    add_subdirectory(sub output EXCLUDE_FROM_ALL) 
    add_executable(test main.cpp)
    

    在父目录下调用cmake .构建之后,在output目录或sub目录下不会出现libsub.a库,说明当指定EXCLUDE_FROM_ALL选项,子目录的目标文件不会生成。

    • 场景4:父目录CMakeLists.txtadd_subdirectory 指定了EXCLUDE_FROM_ALL选项,且父目录的目标文件依赖子目录的目标文件。
    # 父目录下的CMakeLists.txt
    cmake_minimum_required(VERSION 3.10.2)
    project(test)
    
    add_subdirectory(sub output EXCLUDE_FROM_ALL) 
    add_executable(test main.cpp)
    target_link_libraries(test sub)
    

    在父目录下调用cmake .构建之后,在output目录出现libsub.a库,说明即使指定EXCLUDE_FROM_ALL选项,当父目录目标文件对子目录目标文件存在依赖关系时,子目录的目标文件仍然会生成以满足依赖关系。


 最后,以一个完整的例子来结束本文(sub目录下的CMakeList.txttest.htest.cpp等文件内容如上文所示,没有变化),父目录下的main.cppCMakeList.txt如下:

# 父目录下的CMakeLists.txt
cmake_minimum_required(VERSION 3.10.2)
project(test)

include_directories(sub)
add_subdirectory(sub output) 

add_executable(test main.cpp)
target_link_libraries(test sub)
# 父目录下的main.cpp
#include "test.h"
#include <iostream>

int main(int argc, char** argv)
{
    std::cout << "In main..." << std::endl;
    test("hello, world!");
    return 0;
}
# 输出
> cmake --build .
Scanning dependencies of target sub
[ 25%] Building CXX object output/CMakeFiles/sub.dir/test.cpp.o
[ 50%] Linking CXX static library libsub.a
[ 50%] Built target sub
Scanning dependencies of target test
[ 75%] Building CXX object CMakeFiles/test.dir/main.cpp.o
[100%] Linking CXX executable test
[100%] Built target test
>./test
In main...
hello, world!

附录:参考资料

1:https://cmake.org/cmake/help/latest/command/add_subdirectory.html

2:https://www.jianshu.com/p/07acea4e86a3

免责声明:文章转载自《Cmake命令之add_subdirectory介绍》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇彻底理解Android Binder通信架构URL 路由简介下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

go mod

golang 终于出官方版本管理机制,名为 go modules 初体验 使用前: # 先升级 golang 到 1.11 版本,然后 export GO111MODULE=on 在项目github.com/humboldt-xie/test-mod下,通过go mod init go mod init 然后会在当前项目目录下出现 go.mod 文件,内容...

CPU亲和度

CPU亲和度(CPU Affinity),就是将一个进程或者线程强制绑定在CPU的某一个core上运行。 参考:https://www.cnblogs.com/zhangxuan/p/6427533.html https://www.cnblogs.com/LubinLew/p/cpu_affinity.html demo是将ljj_test进程强制绑定在...

命令行下使用javah命令生成.h文件,出现“错误: 无法访问android.app.Activity 找不到android.app.Activity的类文件”的解决方法

在学习NDK中,当我在项目的bin/classes目录下使用javah命令生成头文件时,出现了“错误: 无法访问android.app.Activity 找不到android.app.Activity的类文件”这个问题,如下 跳转到项目的src目录下使用javah命令,而不是在项目的bin/classes目录下使用javah命令即可! 无法访问andro...

上传代码到Gitee忽略部分文件或目录

前言:每次提交代码到Gitee都是整个项目代码提交上去,项目目录里面包括一些文件或者目录是不需要提交的,具体实现如下 一、步骤如下 1.在项目的第一层创建.giignore文件 2.在.giignore文件填写自己要屏蔽的文件和文件夹的语法内容 3.上传代码,不会上传.giignore文件里涉及的文件或者文件夹 二、.giignore里面填写语法规范...

常用文本压缩算法及实现(To be finshed!)

当前仅仅完成了一小部分, 程序上仅仅实现了普通的基于字符的huffman压缩与解压缩. 程序管理上尝试了使用cmake构建,还是很方便的. 测试实验了采用 google test 1.4,也是很好用的. 文档编辑尝试使用latex+cjk, latex2html,相当好用:) 恩,下一步先尝试 python嵌入c++,利用pygraphviz从而可以打印生...

HTML5 本地文件操作之FileSystemAPI整理(一)

一、请求配额 DeprecatedStorageInfo对象 window.webkitStorageInfo:当使用持久存储模式时需要用到该对象的接口 方法: 1.requestQuota(type,size,successCB,errorCB);请求配额 2.queryUsageAndQuota();获取配额信息 window.requestFile...