0

the simple folder structure is like

Root
    CMakeLists.txt
    SubA
        CMakeLists.txt
        some files to be built...
    SubB
        CMakeLists.txt
        some files to be built...

Assuming I have add_library(SubDirLib fooX.cpp) in both SubA/CMakeLists.txt and SubB/CMakeLists.txt where X is A and B respectively, how can I link two libraries SubA/SubDirLib and SubB/SubDirLib in Root/CMakeLists.txt? Note that it will be name conflicted if I call

add_subdirectory(SubA)
add_subdirectory(SubB)

in Root/CMakeLists.txt

victor
  • 655
  • 2
  • 6
  • 7
  • You have no other choice than assign **different** names for both targets. **Why** do you want to have these names identical? (With knowing that we could give you more specific advises). – Tsyvarev Feb 24 '19 at 19:14
  • I think this is similar as what does `namespace` do in c++: developers do not need to worry about confliction beyond local scope since a call can always towards the full namespace path. One concret example is we have a lib called `utils` including many utility functions under said subfolder. Having `SubA/Utils` and `SubB/Utils` is quite common in the parent level, isn't it? – victor Feb 24 '19 at 20:40
  • 1
    No, unlike to C++, targets names in CMake are **global** and should be **unique**. While the project is intended to be built as **standalone**, there is nothing bad in using "common" names (like "utils") for the targets. And many real projects (even widely-used ones) are designed to be built only as standalone. But if you plan to allow using the project with `add_subdirectory`, you need to make its targets unique. E.g. by appending appropriate prefix for them: `LibA-utils`. – Tsyvarev Feb 24 '19 at 20:57

0 Answers0