C++ Header Files and Dependency Management in CMake

When creating targets in CMake using the add_executable or add_library commands, do I need to explicitly specify header files in order to create the dependency tree for automatic build triggering, or does CMake handle this automatically?

For example, consider the following CMake configuration:

cmake_minimum_required(VERSION 3.10)

add_executable(ExampleApp main.cpp utils.cpp)
# Should I list the headers here too?
# target_sources(ExampleApp PRIVATE main.cpp utils.cpp utils.h)

In this example, main.cpp includes utils.h. If I modify utils.h, will CMake automatically detect this change and trigger a rebuild of ExampleApp, or do I need to specify utils.h in the add_executable command or another command to ensure the dependency is tracked?

CMake manage automatically such dependencies. Moreover, for the most common compilers, CMake relies on the compiler itself to discover these dependencies.

Yeah, I see that gnu compilers support dependency discovery (-MD ) whereas with Fortran CMake does the scanning of dependencies. So the header files are completely ignored? or are they useful for IDEs?

Header files, as specified as part of add_library() or add_executable() commands, are not compilable files so are ignored for the compilation step but, as you suggest, will be shown in the IDEs.