Bruce Adams
Bruce Adams

Reputation: 5591

cmake: How can I import a target from a sub-directory to a higher level?

Given a recusive build structure. How can I import a target from a lower level to a higher level? Here is a simplified example:

toplevel CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
add_subdirectory(sub1)

sub1/CMakeLists.txt:

add_subdirectory(subdir)
add_executable(foo EXCLUDE_FROM_ALL
  foo.cpp)

add_custom_target(both)
add_dependencies(both DEPENDS foo bar)

sub1/subdir/CMakeLists.txt:

add_executable(bar EXCLUDE_FROM_ALL
  bar.cpp)

At the toplevel all targets are visible:

At the bottom level (sub1/subdir) only bar is visible:

At the middle level only foo is visible as a target:

make help shows:

The following are some of the valid targets for this Makefile:
... all (the default if no target is provided)
... clean
... depend
... edit_cache
... rebuild_cache
... both
... foo
... foo.o
... foo.i
... foo.s

How can I add bar to this list without moving the build instructions up a level?

Upvotes: 5

Views: 3154

Answers (1)

Emil
Emil

Reputation: 526

If I change

add_dependencies(both DEPENDS foo bar)

to

add_dependencies(both foo bar)

I get the following response to make help in the top folder:

The following are some of the valid targets for this Makefile:
... all (the default if no target is provided)
... clean
... depend
... edit_cache
... rebuild_cache
... both
... foo
... bar

Have a look at the documentation: https://cmake.org/cmake/help/v3.3/command/add_dependencies.html

Upvotes: -1

Related Questions