我有一个关于滥用C预处理器的问题,这个特定的例子与Linux系统上的gcc有关(如果这有所不同).
我想做类似以下的事情:
char filename[] = "hello.txt";
convert_and_include(filename);
Run Code Online (Sandbox Code Playgroud)
其中convert_and_include将.txt的最后三个字符更改为.h,然后将hello.h作为头文件包含在内.我知道这听起来很奇怪,但我保证你有充分的理由想要这样做.
inline void convert_and_include(char filename[]) __attribute__((always_inline))
inline void convert_and_include(char filename[]){
// Error checking has been removed for clarity.
filename[6] = 'h';
filename[7] = '\0';
filename[8] = '\0';
#include AS_STRING(filename) // AS_STRING should evaluate to "hello.h"
}
Run Code Online (Sandbox Code Playgroud)