[英]how to append to __FILE__ at preprocessor time and include the result
I want to include component.h.gen
from component.h
. 我想包括
component.h.gen
从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. 我已经看到我可以将
__FILE__
与#include
一起使用,并且如果没有标题保护,则会导致递归包含。
Is there a way to append a C string literal and include the result? 有没有办法附加C字符串文字并包含结果? 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: 但这会导致包括文件本身在内的文件具有相同的递归-
".gen"
部分未使用-我在MSVC中收到以下警告:
warning C4067: unexpected tokens following preprocessor directive - expected a newline
警告C4067:预处理指令后出现意外的标记-预期换行
Is there a solution that would work with gcc/clang/msvc? 有没有可以与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. 请注意,我打算在数百个(甚至数千个)文件中使用它,并且我想通过复制粘贴相同的代码来简化我的工作,这就是为什么我要使其正常工作。
Unfortunately, this is not possible: 不幸的是,这是不可能的:
__FILE__
expands to a string; __FILE__
扩展为字符串; you cannot unstringify a string. Kind of obscure, but seems to be possible with gcc. 有点晦涩难懂,但似乎可以用gcc实现。
Look at the second answer of this question: 看这个问题的第二个答案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.