CMake 中的兄弟目录

Jon*_*an. 5 c++ makefile cmake

我有一个目录结构如下:

root
  libA
    CMakeLists.txt
    ClassA.cpp
  libB
    CMakeLists.txt
    ClassB.cpp
  sharedCode
    enums.h
    AbstractClass.h
Run Code Online (Sandbox Code Playgroud)

在 CMake 文件中如何包含 sharedCode 目录?这样 classA(在 libA 中)和 classB(在 libB 中)都可以使用 enums.h 和 AbstractClass.h?

在 CMakeLists.txt 我尝试使用:

add_subdirectory(../sharedCode)
Run Code Online (Sandbox Code Playgroud)

但它给出了错误

  add_subdirectory not given a binary directory but the given source
  directory "/root/sharedCode" is not
  a subdirectory of "root/libA".  When
  specifying an out-of-tree source a binary directory must be explicitly
  specified.
Run Code Online (Sandbox Code Playgroud)

Gre*_*ers 3

正如错误所述,当指定当前树之外的相对源目录时,您还必须提供第二个参数来指定生成的二进制文件将放置的目录。

这是以下的文档add_subdirectory

binary_dir 指定放置输出文件的目录。如果它是相对路径,它将根据当前输出目录进行评估,但它也可能是绝对路径。如果未指定binary_dir,则在扩展任何相对路径之前将使用source_dir的值(典型用法)。CMake 将立即处理指定源目录中的 CMakeLists.txt 文件,然后在此命令之后继续对当前输入文件进行处理。