Feature Request: target_include_files()

First post so please let me know if I’ve broken some rule.

Is there any way to add files in the same manner as target include directories but for individual files? If not, would it be possible to add one?

My use case is this. I’m using a lot of templates and concepts and my header files are getting bulky. I really liked having implementation split from definitions so want something like so:

.h file

template <typename T>
T foo(T bar);

#include <name_of_cpp_file>

.cpp file

template <typename T>
T foo(T bar) { return bar; };

currently the include has to look like so: #include "path/to/cpp_file_name"

This is annoying as it makes refactoring a pain later. I also don’t want to add extra directories with just 1 or 2 files and call target_inlcude_directories. I don’t want to just include as my project is a library and I don’t want the user to be able to include the implementation files. I am also building multiple targets and want to keep their included files separate.

Would something like this be doable? I’m happy to help out but would need some guidance as I have not contributed to large open source projects before.

If I understand you correctly, there’s no such mechanism in compilers to do this. All you can tell the compiler is “search this directory for include files”. There’s no way to give a file to be included by the compiler (at least not that stacks well as usage requirements should; it’s intended for PCH and things like that). It also gets ambiguous what’s going on if your target is used by a C (or other language for that matter) TU in some way.

Usually for templates, you have a .tpp (or similar) file that gets included when the implementation is needed rather than just the declaration. There’s really no way to not ship the definition around in practice for templates without exhaustive explicit instantiations.

GCC and Clang have the -include option, though I don’t know if it’s supported on other compilers.

That does something different, though: it forces the inclusion of the file, at the beginning of the translation unit. That wouldn’t work for the OP’s use case, as the implementations would be missing declarations at that point.

I agree, this is really the best way to do what OP is trying to do.