Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: How to prevent using wrong subdirectory CMakeLists directly?

For example, if I have one main CMakeLists that builds all the dependencies of project and then builds project itself via add_subdirectory( project ). But if you try to build via project/CMakeLists you'd fail (because of missing dependencies). SO what is the best way to prevent this scenario, e.g. check if a given CMakeLists was included by the other or if it's a "root".

like image 521
Dan M. Avatar asked Sep 16 '25 23:09

Dan M.


1 Answers

In the CMakeLists.txt file in the subdirectory where this is required, add:

if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
  message(FATAL_ERROR "Blah blah wrong directory")
endif()
like image 141
Mike T Avatar answered Sep 19 '25 13:09

Mike T