CMAKE add sub-directory which is not sub-directory

2019-01-21 22:47发布

问题:

Is It possible to include sibling directory as Sub-Directory inside cmake ?

Something like

A 
  CMakeLists.txt

B
  CMakeLists.txt

and B includes A as sub-directory ?

回答1:

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.



回答2:

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



标签: cmake