I am trying to compile a very simple C program:
# program test.c
#include <stdio.h>
int main()
{
printf("HELLO WORLD \"%s\"\n\n", FOO);
}
and compiled with
gcc -o prog -D FOO=bar test.c
Basically what I am looking to have the following output:
HELLO WORLD "bar"
When I attempt to compile the above, I get the error
<command line>:1:13: note: expanded from here
#define FOO bar
I'm even going so far as to do the following (and yes, I know this is not good):
#indef FOO
define ZZZ sprintf("%s", FOO)
#endif
#define FOO bar makes no sense in your example; what is the bare word bar?
If you want to replace it with the string "bar", then you need to produce
#define FOO "bar"
You can achieve this by adding quotes to your CLI flag, which typically should be escaped:
gcc test.c -D FOO=\"bar\" -o prog
./prog
HELLO WORLD "bar"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With