Sometimes we could get a targets names clash while using add_subdirectory()
So the most logical way is to add all inner targets at the namespace (as it was solved in C++ at the language level).
I propose a new syntax for
add_subdirectory(<subdir> NAMESPACE <namespace>) to adding all created targets inside that directory to the namespace , e.g., consider this tree hierarchy:
CMakeLists.txt main.cpp liba/CMakeLists.txt libb/CMakeLists.txt
# liba and libb both contain the targets "client", so it will be an error in CMake to use it simultaneously. # But with the NAMESPACEs this problem seems to be solved. add_subdirectory(liba NAMESPACE LibA) add_subdirectory(libb NAMESPACE LibB) add_executable(exec_with_ab main.cpp) target_link_libraries(exec_with_ab LibA::client LibB::client)
N.B.: inside the subdir there are no any introduced namespaces!
It also could be extended to some another commands which could add the targets with the same names, e.g. fetch_content()