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.
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:
FYI: Apparently even different versions of the same toolchain on the same system can act differently in this regard...
(As in, it would seem this would be a shell-passing issue, but apparently it's not limited to merely the shell).
Here we have xc32-gcc 4.8.3 vs. (avr-)gcc 4.7.2 (and several others)
using the same makefile and main.c, the only difference being 'make CC=xc32-gcc', etc.
CFLAGS += -D'THING="$(THINGDIR)/thing.h"' has been in-use on many versions of gcc (and bash) over several years.
In order to make this compatible with xc32-gcc (and in light of another comment claiming that \" is more portable than '"), the following had to be done:
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";
I just found that one of our applications does not compile on Ubuntu. And since Linux and Windows didn't agree on a common approach, I used this:
NAME := "Mary"
ifeq ($(SystemRoot),)
# building on another OS
CFLAGS_ADD += -Dname=\"Mary\"
else
# building on Windows
CFLAGS_ADD += -Dname=\\\"Mary\\\"
endif
It worked for me for VS Code on both Mac and Windows. Other methods described here like "-Dname=\"SHERAJ\"" and "-Dname=\\\"SHERAJ\\\"" did not work for me.