c++ - 带有子库和包含目录的库,我如何在 CMake 中做到这一点?

标签 c++ cmake

我有一个这样结构的项目:

project
 |\_subdir
 |   | Some .cxx files
 |   | some .h files (included with #include "foo.h"
 |    \_subdir
 |       | Some .h files (included with #include <subdir/foo.h>)
  \_subdir2
     | some .cxx files
     | some .h files (included with #include "foo.h"
      \_subdir2
         | Some .h files (included with #include <subdir2/foo.h>)

我需要在这里做一些事情:

  1. subdir/subdir目录应该公开可用于使用此库的程序 #include <subdir/foo.h> .
  2. subdir目录应该生成自己的静态链接库,以便我可以运行测试,确保维护不同子目录之间的依赖关系。
  3. 整个事情应该生成一个共享库或静态库,其中包含 subdir 中的所有内容。目录,并且可以通过使用此库的程序链接到。

目前,我有一个使用 autotools(但不是 automake)制作的构建系统,以及一些自定义的 perl 脚本,这些脚本处理来自 -M 标志的依赖项输出并创建表达依赖项信息的自定义 makefile。此外,他们创建了一个包含目录,该目录是一个 linkfarm,以便每个人都可以使用 #include <subdir/foo.h>只是 -I linkfarm 目录。

我想将其转换为 CMake,但我不知道如何处理子目录结构和包含。如果绝对必要的话,我可以稍微重新安排目录结构,但我非常希望保留各个子目录的伪独立性,部分原因是能够确保子库具有特定的依赖关系作为保持模块化和整洁的一种方式。

我该如何在 CMake 中执行此操作?我是否已移动subdir/subdir目录?

因为看起来如果我添加subdir目录作为公共(public)包含,那么事情就会变得非常糟糕,因为人们会在 subdir 看到私有(private) .h 文件。级别以及能够包含其中的 .cxx 文件。更糟糕的是,他们会将它们视为顶级包含(即#include <bar.cxx>,其中bar.cxx是一个私有(private)实现文件,其目标代码应该已经在库中)。

但是如果添加subdir/subdir作为包含目录,.h文件不会像应有的那样出现在子目录中(即 #include <subdir/public.h> 不起作用,相反我必须使用 #include <public.h> 这根本不是我的意图)。

最佳答案

如果您担心库的用户,他们将看到已安装文件,而不是/构建<中的文件/em> 目录。如果您不安装私有(private) header (来自 subdir/),用户将看不到它们。

但是如果您不打算安装库,那么您可以分离公共(public)和私有(private)包含树。例如。每个项目都可以直接在其源目录中拥有私有(private) header ,并在其include/子目录下拥有公共(public) header :

  project
 |\_subdir
 |   | Some .cxx files
 |   | Private .h files (included with #include "foo.h")
 |    \_include (public include directory)
 |       \_subdir
 |         | Public .h files (included with #include <subdir/foo.h>)
  \_subdir2
     | some .cxx files
     | Private .h files (included with #include "foo.h")
      \_include (public include directory)
         \_subdir2
           | Public .h files (included with #include <subdir2/foo.h>)

关于c++ - 带有子库和包含目录的库,我如何在 CMake 中做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50589482/

相关文章:

c++ - 我如何检查该函数是否真的获得了一个定义为 const 的变量?

c++ - 警告 : control may reach end of non-void function [-Wreturn-type]

windows - 在Windows上安装dlib期间发生Cmake错误?

c++ - 为什么需要在此模板中使用 typedef?

c++ - std::basic_fstream::put() 无效

cmake - 如何使 CMake 'FILE' 命令依赖于 TARGET 或 OUTPUT?

CMake 使用相对路径导入共享库链接,中断安装

cmake - 使用 cmake 生成构建目标时找不到 CONAN_PKG::<PKG-NAME>

linux - CMake - set_property 找不到目标 xxx。也许它还没有被创造

c++ - 如何检查 3D 点是在 vector 的左侧还是右侧