Search code examples
windowscmdcmakecmake-gui

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)


I'm working on 3dtk which uses CMake to build. You can find its CMakeLists.txt here: https://sourceforge.net/p/slam6d/code/HEAD/tree/trunk/CMakeLists.txt

In several places it uses the add_subdirectory directive and never supplies a second argument (the binary_dir).

This seems to be no problem when calling ccmake out-of-tree on linux or when using cmake-gui on windows as in both cases, the project configures successfully.

But when I run plain cmake on windows in the command prompt, then I get several errors like this:

CMake Error at Z:/CMakeLists.txt:471 (add_subdirectory):
  add_subdirectory not given a binary directory but the given source
  directory "Z:/src/slam6d" is not a subdirectory of "Z:/". When specifying
  an out-of-tree source a binary directory must be explicitly specified.

I suspect that I'm just missing an option to the cmake command but which one?

Just as with cmake-gui on Windows where I specify a different path for Where to build the binaries than for where is the source code to make an out-of-tree build and just as with ccmake on Linux where I run ccmake in a subdirectory of my source like this ccmake .., I'm running cmake on Windows from within a subdirectory of my source.

So what are cmake-gui and ccmake doing that cmake does not and where manual intervention is required?

Edit: after finding the solution it turns out that the problem was that my source directory was directly a windows drive letter instead of a subdirectory of one.


Solution

  • 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: