Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a dependency not in a subdirectory using CMake

Tags:

cmake

Let's say there's following directory structure:

root
  |
  +--projects
  |      |
  |      +-test
  |         |
  |         +-CMakeFiles.txt
  |
  +--libs
       |
       +-testlib
            |
            +-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

add_subdirectory("../../libs/testlib")

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.

like image 603
SigTerm Avatar asked Apr 04 '12 02:04

SigTerm


People also ask

What is add_ subdirectory in CMake?

add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL] [SYSTEM]) Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists. txt and code files are located.

How do I add a library to CMake project?

To add a library in CMake, use the add_library() command and specify which source files should make up the library. Rather than placing all of the source files in one directory, we can organize our project with one or more subdirectories.


1 Answers

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
like image 56
Fraser Avatar answered Sep 22 '22 22:09

Fraser