如何定义字符串中的gcc命令行的文字? [英] How to define a string literal in gcc command line?
本文介绍了如何定义字符串中的gcc命令行的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在GCC命令行中,我想定义一个字符串,如 -Dname =玛丽
,然后在源$ C $ C我想的printf(%S,姓名);
打印玛丽
结果
我怎么能这样做呢?
In gcc command line, I want to define a string such as -Dname=Mary
, then in the source code I want printf("%s", name);
to print Mary
.
How could I do it?
推荐答案
两个选项。首先,逃避引号使shell不会吃他们:
Two options. First, escape the quotation marks so the shell doesn't eat them:
gcc -Dname=\"Mary\"
或者,如果你真的想-Dname =玛丽,你可以stringize它,虽然它有点哈克。
Or, if you really want -Dname=Mary, you can stringize it, though it's a bit hacky.
#include <stdio.h>
#define STRINGIZE(x) #x
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
int main(int argc, char *argv[])
{
printf("%s", STRINGIZE_VALUE_OF(name));
}
注意STRINGIZE_VALUE_OF将愉快地评价到宏的最终定义。
Note that STRINGIZE_VALUE_OF will happily evaluate down to the final definition of a macro.
这篇关于如何定义字符串中的gcc命令行的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文