Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

cmake

I looked at this post: Using CMake to statically link to a library outside of the project. But I'm still having trouble interpreting what this means:

add_subdirectory(/path/to/the/library/source/directory subproject/grzeslib)

I'm assuming "/path/to/the/library/source/directory" means the path from the hard drive, but I don't understand what "subproject/grzeslib" means. Now I tried:

include_directories(../path/to/dir)
add_subdirectory (../path/to/dir .) 

But I'm getting an elaborate warning. Is there a better way to do this?

like image 636
user3063750 Avatar asked Feb 07 '16 23:02

user3063750


People also ask

How do I get out of source build?

To use out of source builds, create a build directory in your top-level folder (technically, this can be anywhere, but the top-level project folder seems to be a logical choice). Next, change into your build directory and run cmake pointing it to the directory of the top-level CMakeLists. txt.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is Cmake_source_dir?

The path to the top level of the source tree. This is the full path to the top level of the current CMake source tree. For an in-source build, this would be the same as CMAKE_BINARY_DIR .


2 Answers

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.
like image 145
FrankM Avatar answered Sep 29 '22 14:09

FrankM


From the documentation

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

From your example, all the binaries created in "/path/to/the/library/source/directory" will be placed in "subproject/grzeslib", it's a good thing to keep "clean" the source dirs.

like image 30
Joel Avatar answered Sep 29 '22 16:09

Joel