5

I have a directory structure as follows:

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

In the CMake file how can include the sharedCode directory? So that both classA (in libA) and classB (in libB) can use the enums.h and AbstractClass.h?

In the CMakeLists.txt I tried using:

add_subdirectory(../sharedCode)

but it gives the error

  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.
starball
  • 20,030
  • 7
  • 43
  • 238
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • 2
    Even if you solve the given error by adding the binary directory parameter, your `sharedCode` directory doesn't seem to have its own `CMakeLists.txt` (which maybe would be an overkill if it contains only headers). So I would just do `include_directories(../sharedCode)`. – Florian Nov 24 '15 at 20:29
  • 1
    It could include cpp files – Jonathan. Nov 25 '15 at 16:10
  • Ok, then you have a similar directory structure I have in one of my projects. For more details on possible CMake solutions reflecting your setup see e.g. [CMake: How to setup Source, Library and CMakeLists.txt dependencies?](http://stackoverflow.com/questions/31512485/cmake-how-to-setup-source-library-and-cmakelists-txt-dependencies) or [CMake share library with multiple executables](http://stackoverflow.com/questions/33443164/cmake-share-library-with-multiple-executables) – Florian Nov 25 '15 at 19:28

1 Answers1

3

As the error says, when specifying a relative source directory outside of the current tree, you must also supply a 2nd argument that specifies the directory where the produced binary file will be placed.

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.

gpanders
  • 1,301
  • 2
  • 15
  • 23
  • Ah I read the error as you can't specify an out of tree source directory, only an out of tree binary directory. – Jonathan. Nov 25 '15 at 16:11
  • Why do I need to provide the second argument, I figured it was because it had to be a subdirectory, but I just did add_subdirectory(../sharedCode ../sharedCode) and it worked – Jonathan. Nov 25 '15 at 16:13
  • now even `../sharedCode` even needs a CMakeLists.txt file? This is completely inefficient by CMake. Just create an argument that can include a directory of shared sources in the chain. That's it. Python does it for all their imports directly in the script. – Brian Wiley Jan 04 '22 at 04:06