How to Explicitly specify cmake to process a different .txt file

Is it possible to have two List.txt files , i,e, CMakeLists1.txt and CMakeLists2.txt in a single directory and while compiling is it possible to inform cmake to consider CMakeLists2.txt for further process ? If yes what is flag should be used ?


No, it is not possible. Only files named CMakeLists.txt are considered when command add_subdirectory() is evaluated.

But you can manage similar behavior with include() command and CMake command line argument:

# file CMakeLists.txt
  include (${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists2.txt)

If you want to process extra file:


@marc.chevrier Thanks for that.

Suppose i have only one directory with CMakeLists.txt and CMakeLists1.txt

So in the command line can i do something like cmake --CMakeList1.txt … instead of the default CMakeLists.txt ?

Not at all. Option -C is to populate the cache (file CMakeCache.txt).

Again, there is no way to use alternate names to CMakeLists.txt.

OK , i should be thinking alternate way to deal the requirement.

Thanks Marc.

In situations where I want to automatically choose the Generator (say Ninja vs. Make) or other things that I can or cannot do within CMakeLists.txt, I use ctest -S script.cmake scripts. An example .cmake script I use across many projects is

  • looks for new-enough Ninja first, with Make fallback
  • allows user to type single command to build and test
  • control compiler preference order
  • run tests in parallel

These are all doable by other means, but this is a single command that new users don’t have to be concerned with the details if it works.