How do I pass a quoted string with -D to gcc in cmd.exe?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 04:24:16

问题


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?


回答1:


What's wrong with

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


来源:https://stackoverflow.com/questions/9928595/how-do-i-pass-a-quoted-string-with-d-to-gcc-in-cmd-exe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!