49

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?

Community
  • 1
  • 1
user3063750
  • 833
  • 1
  • 9
  • 13

3 Answers3

43

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.
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
FrankM
  • 772
  • 7
  • 11
  • 2
    Okay thanks, but I'm not quite sure how to decide on where to put the output files. – user3063750 Feb 07 '16 at 23:59
  • So if I use "add_subdirectory (../path/to/dir .) " why aren't binary files added to the current directory? Should the binary_dir be the build folder? – user3063750 Feb 08 '16 at 00:08
  • I made binary_dir my build folder and that got rid of the warning. – user3063750 Feb 08 '16 at 00:17
  • it didn't solve error I received "add_subdirectory not given a binary directory but the given source directory" , first answer at "https://stackoverflow.com/questions/50408169/cmake-error-add-subdirectory-not-given-a-binary-directory" solved the problem, be adding 'build' to the command – GM1 Feb 06 '19 at 11:25
3

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.

Joel
  • 1,805
  • 1
  • 22
  • 22
  • I'm getting a warning from CMake that the above code I added isn't a good idea. Where should I put the output files? – user3063750 Feb 08 '16 at 00:00
2

https://stackoverflow.com/a/15570165/2482283

need specifiy second parameter binary path, it is relative to ${CMAKE_CURRENT_BINARY_DIR}

add_subdirectory(/path/to/the/library/source/directory subproject/grzeslib)
andrewchan2022
  • 4,953
  • 45
  • 48