如何在cmd.exe中将具有-D的引号字符串传递给gcc? [英] How do I pass a quoted string with -D to gcc in cmd.exe?

查看:473
本文介绍了如何在cmd.exe中将具有-D的引号字符串传递给gcc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 gcc (可能还有其他编译器),可以在源文件之外定义一个宏,如下所示:

  c:\MinGW\bin\gcc.exe -DEFEFINED_INT = 45 foo.c -o foo.exe 

这会将 DEFINED_INT 定义为45,可以在编译时看到

  #include< stdio.h> 

int main(){
printf(The define was:%d \\\
,DEFINED_INT);

返回0;
}

编译后的 foo.exe 将在执行时打印 45



现在,我不知道如何传递一个引用的字符串而不是int。因此,printf会像

  printf(The define was:%s\\\
,DEFINED_STRING);

以及编译方式如下:

  c:\MinGW\bin\gcc.exe -DDEFINED_STRING =foo bar bazfoo.c -o foo.exe 

但这不起作用,相应的行将会是

  printf(The define was:%s\\\
,foo bar baz);

(即:没有所需的引号)。

  c:\MinGW\bin\gcc.exe -DDEFINED_STRING = \foo bar baz\foo.c -o foo.exe 

都不起作用,因为gcc现在试图找到 baz (错误信息是:

  gcc.exe:bar:没有这样的文件或目录
gcc.exe:baz:无效参数
<命令行>:0:16:警告:缺少终止字符

所以,我怎样才能让编译器和cmd.exe一起做我想做的事?


With gcc (and possibly other compilers as well) it's possible to define a macro outside the source file like so:

c:\MinGW\bin\gcc.exe -DDEFINED_INT=45 foo.c -o foo.exe

This will define DEFINED_INT to 45 which can be seen when compiling

#include <stdio.h>

int main() {
  printf("The define was: %d\n", DEFINED_INT);

  return 0;
}

The compiled foo.exe will then print 45 when executed.

Now, what I have no clue of is how do I pass a quoted string instead of an int. So, the printf would then be something like

  printf("The define was: %s\n", DEFINED_STRING);

and the compilation like so:

c:\MinGW\bin\gcc.exe -DDEFINED_STRING="foo bar baz" foo.c -o foo.exe

but this doesn't work, the respective line will be

printf("The define was: %s\n", foo bar baz);

(that is: without the desired quotes).

c:\MinGW\bin\gcc.exe -DDEFINED_STRING=\"foo bar baz\" foo.c -o foo.exe

doesn't work, neither, as gcc now tries to find the files bar and baz (the error message is:

gcc.exe: bar: No such file or directory
gcc.exe: baz": Invalid argument
<command-line>:0:16: warning: missing terminating " character

So, how can I get the compiler along with cmd.exe to do what I want?

解决方案

What's wrong with

-DDEFINED_STRING="\"foo bar baz\""

这篇关于如何在cmd.exe中将具有-D的引号字符串传递给gcc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆