c – CMake中的同级目录

我有一个目录结构如下:

root
  libA
    CMakeLists.txt
    ClassA.cpp
  libB
    CMakeLists.txt
    ClassB.cpp
  sharedCode
    enums.h
    AbstractClass.h

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

在CMakeLists.txt中,我尝试使用:

add_subdirectory(../sharedCode)

但它给出了错误

  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.

最佳答案 如错误所示,在指定当前树之外的相对源目录时,还必须提供第二个参数,该参数指定将生成二进制文件的目录.

Here’s the documentation for add_subdirectory:

The binary_dir specifies the directory in which to place the output files. If it is a relative path it will be evaluated with respect to the current output directory, but it may also be an absolute path. If binary_dir is not specified, the value of source_dir, before expanding any relative path, will be used (the typical usage). The CMakeLists.txt file in the specified source directory will be processed immediately by CMake before processing in the current input file continues beyond this command.

点赞