Reputation: 4538
I want to include component.h.gen
from component.h
.
I've already seen that I can use __FILE__
with #include
and that results in recursive inclusion if there is no header guard.
Is there a way to append a C string literal and include the result? This is what I've tried so far:
#define CAT_IMPL(s1, s2) s1##s2
#define CAT(s1, s2) CAT_IMPL(s1, s2)
#define DO_IT CAT(__FILE__, ".gen")
#include DO_IT
But this results in the same recursion with the file including itself - the ".gen"
part is not used - and I get this warning with MSVC:
warning C4067: unexpected tokens following preprocessor directive - expected a newline
Is there a solution that would work with gcc/clang/msvc?
Note that I'm planning on using this in hundreds if not thousands of files and I would like to simplify my life by just copy-pasting the same code - that's why I'm trying to get this to work.
Upvotes: 1
Views: 526
Reputation: 2674
Unfortunately, this is not possible:
__FILE__
expands to a string; you cannot unstringify a string. So the technique of just adding the "rest" of the string, then stringifying the result, isn't available.Upvotes: 3
Reputation: 433
Kind of obscure, but seems to be possible with gcc.
Look at the second answer of this question:
Upvotes: 1