target_include_directories() vs headers in target_sources()

Looking for advice on how to think about when to add directories with headers in them as target_include_directories() vs adding every header file by hand using target_sources().

One thing I’ve noted is that I struggle to tell the linker which one to use if different headers with the same name (say utils.h) exists on different levels in a code base. Is target_sources() the most practical solution in that case?
Would appreciate some rules of thumb type advice on this, thanks.

target_include_directories is relevant to the preprocessor (which is usually part of the compiler), since it allows to resolve #include preprocessor directives.

Adding headers with target_sources is relevant to IDEs (Visual Studio, Xcode, etc.) so they can display these header files, even if they are not directly compiled.

So these 2 functions are doing quite different things, and I can’t think of any situation where you would have to choose between them.

I hope this helps.

1 Like