Replace #define X macro value with another one spe

2020-02-07 04:47发布

问题:

Suppose I have this code:

#include<stdio.h>

#ifndef STR
  #define STR "HELLO"
#endif

int main()
{
    printf(STR "WORLD \n");
    return 0;
}

Which basically translates to: if STR was not defined, then define it to be "HELLO", so the program output will be

HELLO WORLD

Now, my question is: how can I modify this STR value when compiling using gcc? I want to change "HELLO" to "HI", for example, without changing anything in the source (program.c) file.

What is the gcc syntax to do so?

I've tried

gcc -Wall program.c -DSTR="HI" -o program

but it didn't produce the expected output.

Any suggestions?

Thanks!

回答1:

Try in the form of:

-DSTR=\"MyString\"


标签: c gcc macros