0

I'm not an experimented CMake user, I'm currently trying to include an existing project to a top project and the CMakelists.txt of the top level project is very simple:

cmake_minimum_required(VERSION 2.8)

project(A)

add_subdirectory(B)

As a mater of testing gradually at this point, I wanted to see if the targets defined in project B are building. So running a make all from the build directory of project A builds correctly, but some target which are not part of all fail to build from project A build directory, where I'm getting link errors with "undefined reference", while the same target could build successfully from the project B' build directory. Could somebody explain please what the issue could be?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Nerc Sa
  • 1
  • 1
  • "Could somebody explain please what the issue could be?" - It is impossible to say what is wrong without viewing the **code** of the project `B`, error message and so on. See [ask]. Note, that not every project could be built when added with `add_subdirectory`. – Tsyvarev Feb 23 '20 at 07:53

1 Answers1

0

Adding the sub-directory only causes cmake to process the CMakeLists.txt file within that sub-directory, it does not impose any dependency on the target you're building.

It might help to think of this process as just building one great big set of rules (for whatever build system it's translating to) from all the CMakeLists files you add.

However, that will only place rules for all the individual CMakeLists targets in that resulting ruleset, so that you can build those targets by name. If you choose to build a target in A that you have not stated depends on B (even though it may actually depend on it), then B will not be used.

To do that, you'll have to set up linker dependencies, something like (depending on names and what B generates, of course):

cmake_minimum_required(VERSION 2.8)
project(A)
add_subdirectory(B)
target_link_libraries(A B)
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953