Cmake Error: "Add_Subdirectory Not Given a Binary Directory"

CMake Error: add_subdirectory not given a binary directory

The error message is clear - you should also specify build directory for googletest.

# This will build googletest under build/ subdirectory in the project's build tree
add_subdirectory( ${GOOGLETEST_PROJECT_LOCATION} build)

When you give relative path (as a source directory) to add_subdirectory call, CMake automatically uses the same relative path for the build directory.

But in case of absolute source path (and when this path isn't in your source tree), CMake cannot guess build directory, and you need to provide it explicitly:

See also documentation for add_subdirectory command.

CMAKE add sub-directory which is not sub-directory on real directory

It is possible, although perhaps not recommended...

You can use the two-argument form of the add_subdirectory command to add any directory you want as a "sub" directory:

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../A ${CMAKE_CURRENT_BINARY_DIR}/A)

The second argument of the two-argument form specifies where to put the binary directory for the added subdirectory.

You just have to be careful that there's not also another real sub-directory of B that is also named "A" and that is also add_subdirectory'd... Because if you do, then that would be an error, as CMake cannot have two different source directories mapping into the same build directory.

How do I explicitly specify an out-of-tree source in CMake?

The second parameter is output directory for the results of the targets from that subdirectory.

From the documentation here: https://cmake.org/cmake/help/v3.3/command/add_subdirectory.html

add_subdirectory

Add a subdirectory to the build.

add_subdirectory(source_dir [binary_dir]
[EXCLUDE_FROM_ALL])

Add a subdirectory to the build.

  • The source_dir specifies the directory in which the source CMakeLists.txt and code files are located. If it is a relative path it
    will be evaluated with respect to the current directory (the typical
    usage), but it may also be an absolute path.
  • 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.
  • If the EXCLUDE_FROM_ALL argument is provided then targets in the subdirectory will not be included in the ALL target of the parent
    directory by default, and will be excluded from IDE project files.
    Users must explicitly build targets in the subdirectory. This is meant
    for use when the subdirectory contains a separate part of the project
    that is useful but not necessary, such as a set of examples. Typically
    the subdirectory should contain its own project() command invocation
    so that a full build system will be generated in the subdirectory
    (such as a VS IDE solution file). Note that inter-target dependencies
    supercede this exclusion. If a target built by the parent project
    depends on a target in the subdirectory, the dependee target will be
    included in the parent project build system to satisfy the dependency.

CMake add_subdirectory given source which is not an existing directory

The error complains that the directory olympic/firebase_cpp_sdk does not exist. From the documentation for add_subdirectory(), the first argument to this command can be a relative or absolute path:

If it is a relative path it will be evaluated with respect to the current directory (the typical usage), but it may also be an absolute path.

Since you have provided a relative path, CMake will look for olympic/firebase_cpp_sdk relative to the current CMakeLists.txt file being processed; this path does not exist on your system. To ensure CMake can locate the firebase_cpp_sdk directory, try specifying an absolute path instead, as suggested in the tutorial in this step:


  1. Specify the location of the unzipped SDK in your project's gradle.properties file:

     systemProp.firebase_cpp_sdk.dir=full-path-to-SDK

So try the full path:

systemProp.firebase_cpp_sdk.dir=/your/full/path/to/SDK

CMake add_subdirectory()

After three days trying everything, I finally found the answer... Sir DLRdave was right actually: the problem was not from the code itself but from something "out of the code".

Problem found:

I created and edited all my files with Notepad++. Actually when opening the files with windows notepad (because i was curious) a strange rectangle symbol appeared and the file did not look like the one I usually see on Notepad++
I found out that the symbol was a "\n\r" that Notepad++ did not show me (it must be filtered) but going on windows notepad, you could see that the whole file was "impure" and appeared on a single line instead of the layout i saw on Notepad++.

As this "encoding" bug appeared only in the subdirectory CMakeLists, it could not be read but said no error when interpreting with CMake and that's probably why I had no returned error from running CMake.

Solution:

I used the native2ascii.exe tool from Java to correct the encoding error.

The why:

Actually what it probably means is that the syntaxic parser of CMake has probably not been designed for filtering this type of char appearing with strange encoding, that's why it gave me 3 days of intense debugging.

cmake add_subdirectory error only with cmake command line but not cmake-gui on windows (with source checked out at root of a drive letter)

It turns out that this is a bug in cmake. Quoting David Cole:

Put your source in a sub-directory. CMake simply does not work at the
root of a drive letter on Windows. CMakeLists.txt MUST be in at least
one sub-directory underneath a root drive letter path.

There are also some open bugreports about this issue:

  • http://www.cmake.org/Bug/view.php?id=15134
  • http://www.cmake.org/Bug/view.php?id=10072


Related Topics



Leave a reply



Submit