我有一个这样结构的项目:
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>)
我需要在这里做一些事情:
subdir/subdir
目录应该公开可用于使用此库的程序#include <subdir/foo.h>
.subdir
目录应该生成自己的静态链接库,以便我可以运行测试,确保维护不同子目录之间的依赖关系。- 整个事情应该生成一个共享库或静态库,其中包含
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/