问题
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