在Windows下,我有一个包含Windows样式路径的环境变量.我想在我的程序中构建该路径并将其打印出来.因此,如果我的路径是c:\ top,我使用-DTOP = $(TOP)将其传递给编译器.注意我在将它传递给编译器之前无法将其转换为c:\\ top.
现在,我有相当于:
#define TOP=c:\top
Run Code Online (Sandbox Code Playgroud)
我想要相当于:
char path[]="c:\\top";
Run Code Online (Sandbox Code Playgroud)
我不能只使用stringafication运算符:
#define WRAP1(X) #X
#define WRAP(X) WRAP1(X)
char path[] = WRAP(TOP);
Run Code Online (Sandbox Code Playgroud)
这只会导致字符串"c:\ top",编译器将其视为转义序列(即\ t).
我认为一个可能的解决方案是构建一个字符串文字,但其他解决方案也可以.有没有办法使用宏来构造一个可以产生的字符串文字:
char path[] = R"foo(c:\top)foo";
Run Code Online (Sandbox Code Playgroud)
到目前为止,由于涉及"或()或\"的变化的原因,我的所有尝试都失败了.
谢谢.