How to define a string literal in gcc command line?

71,210

Solution 1

Two options. First, escape the quotation marks so the shell doesn't eat them:

gcc -Dname=\"Mary\"

Or, if you really want -Dname=Mary, you can stringize it, though it's a bit hacky.

#include <stdio.h>

#define STRINGIZE(x) #x
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)


int main(int argc, char *argv[])
{
    printf("%s", STRINGIZE_VALUE_OF(name));
}

Note that STRINGIZE_VALUE_OF will happily evaluate down to the final definition of a macro.

Solution 2

to avoid the shell "eating" the quotes and other characters, you might try single quotes, like this:

gcc -o test test.cpp -DNAME='"Mary"'

This way you have full control what is defined (quotes, spaces, special characters, and all).

Solution 3

Most portable way I found so far is to use \"Mary\" - it will work not only with gcc but with any other C compiler. For example, if you try to use /Dname='"Mary"' with Microsoft compiler, it will stop with an error, but /Dname=\"Mary\" will work.

Solution 4

In Ubuntu I was using an alias that defines CFLAGS, and CFLAGS included a macro that defines a string, and then I use CFLAGS in a Makefile. I had to escape the double quote characters and as well the \ characters. It looked something like this:

CFLAGS='" -DMYPATH=\\\"/home/root\\\" "'

Solution 5

This is my solution for : -DUSB_PRODUCT=\""Arduino Leonardo\""
I used it in a makefile with:
GNU Make 3.81 (from GnuWin32)
and
avr-g++ (AVR_8_bit_GNU_Toolchain_3.5.0_1662) 4.9.2

The results in a precompiled file (-E option for g++) is:
const u8 STRING_PRODUCT[] __attribute__((__progmem__)) = "Arduino Leonardo";

Share:
71,210
richard
Author by

richard

Updated on July 08, 2022

Comments

  • richard
    richard almost 2 years

    In gcc command line, I want to define a string such as -Dname=Mary, then in the source code I want printf("%s", name); to print Mary.
    How could I do it?

  • richard
    richard about 14 years
    thank you so much Arthur. you must be a expert in C. further question: I perfer the second option. when I'm using STRINGIZE_VALUE_OF(name), it translate it to "1", in the case that I have gcc -Dname=Mary -DMary. is there anyway to let gcc stop interprite Mary
  • Arthur Shipkowski
    Arthur Shipkowski about 14 years
    Richard, after much review I do not believe I can come up with a way that works in the above example. Unfortunately, your choices are no expansion (e.g. gives you "name") and full expansion (e.g. name->Mary->1 if Mary is defined as 1). Depending on your exact usage case there may be ways around this -- if Mary can become a const int rather than a define, for example.
  • Ionoclast Brigham
    Ionoclast Brigham about 10 years
    Can anyone give a rationale for why you need to use nested stringification macros like that? It seems like the result should be the same, but calling STRINGIZE_VALUE_OF() seems to force macro expansion of the argument, while STRINGIZE() doesn't.
  • Arthur Shipkowski
    Arthur Shipkowski over 9 years
    @IonoclastBrigham, didn't see this until today. Part of this is that sometimes you want to stringize barewords -- for example, in many cases stringize is used to implement assert() such that it can print out the exact expression you have -- in which case you want to have macros unexpanded. Once you realize a base stringize works that way, the nesting forces a second round of macro expansion.
  • Den-Jason
    Den-Jason over 6 years
    Sometimes, both single escaping and wrapping in single quotes didn't work, but this did. Other times, it didn't. I think the difference is whether the flags are put in quotes overall: 1) DEFINES=-DLOGPATH=\"./logfile\" CFLAGS = -v $(DEFINES).... 2) DEFINES=-DLOGPATH=\\\"./logfile\\\" CFLAGS = "-v $(DEFINES)...." Using the -v compiler option is useful to see what the preprocessor is doing.
  • Lassi
    Lassi over 4 years
    This is a good example of defines that are used like booleans. However the OP asked about string defines which are more tricky.