CMAKE add sub-directory which is not sub-directory on real directory

It is possible, although perhaps not recommended...

You can use the two-argument form of the add_subdirectory command to add any directory you want as a "sub" directory:

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../A ${CMAKE_CURRENT_BINARY_DIR}/A)

The second argument of the two-argument form specifies where to put the binary directory for the added subdirectory.

You just have to be careful that there's not also another real sub-directory of B that is also named "A" and that is also add_subdirectory'd... Because if you do, then that would be an error, as CMake cannot have two different source directories mapping into the same build directory.


Unfortunately, no.

As solution i may suggest you to add_subdirectory(A) and add_subdirectory(B) at the top level and set vars you want to export from A with PARENT_SCOPE. This would allow B/CMakeLists.txt to access varibles defined in A/CMakeLists.txt