Add a dependency not in a subdirectory using CMake


Let's say there's following directory structure:

  |      |
  |      +-test
  |         |
  |         +-CMakeFiles.txt

test contains CMakeFiles.txt and testlib also contains CMakeFiles.txt. "test" produces an executable and "testlib" produces a static library.

I want "test" to link with "testlib" without using symlinks and without moving "testlib" library into a subdirectory within "test".

Because "testlib" isn't a subdirectory of "test", I can't do


In test's CMakeFiles.txt – CMake will complain about "testlib" not being in the "test" subdirectory.

Also, because system has several different compilers, I can't simply install "testlib" libraries into some kind of central directory, so I want test to compile a local copy of testlib and link with it (i.e. as if testlib was a subdirectory). I also want the "test" project to automatically rebuild "testlib" if it has been changed.

So, how can I deal with it? I am using CMake 2.8.4 on Windows XP SP3.

Best Solution

You could either provide a top-level CMakeLists.txt in root, or provide a binary directory to the add_subdirectory command; e.g.

add_subdirectory("../../libs/testlib" "${CMAKE_CURRENT_BINARY_DIR}/testlib_build")

This creates a subdirectory called testlib_build in your current build directory which contains the generated project files for testlib, but not the source.

For further info, run

cmake --help-command ADD_SUBDIRECTORY
Related Question