To my understanding, CMake takes all the sources that are specified in add_executable (or add_library), and creates a makefile target to compile and link each one to create the resultant executable (or library). However, I see online that it is allowed, and even somewhat encouraged, to specify headers in add_executable/add_library! This surprises me because my intuition says that, if headers are specified there, they will be compiled and linked just like a regular source file, which would be wrong,...but somehow they are not; why?
Does CMake check the file extension of provided sources and filter out all the .h/.hh/.hpp/etc.? Or does CMake detect and filter out sources which are #include'ed in other sources? Or does CMake have some other way of knowing which sources should or should not be compiled?
For example, how does
add_executable(myprogram myprogram.cpp myprogram.hpp)
not cause
g++ -c myprogram.hpp -o myprogram.hpp.o
?
I found the following related SO discussions that generally suggest that headers may be specified within add_executable/add_library, but they do not answer why the headers are not then compiled: