Daivid
Daivid

Reputation: 677

How to create a macro string

I'm trying to define a string macro before compiling my C code. I've tried something like:

#include <stdio.h>

int main(void) {
    printf("%s", AMEM);
    return 0;
}

and I've tried to compile with:

gcc -D AMEM="Deus Abencoa" file.c

But I keep getting this message:

file.c:5:15: note: in expansion of macro ‘AMEM’
  printf("%s", AMEM);
               ^
<command-line>:0:4: note: each undeclared identifier is reported only once for each function it appears in
file.c:5:15: note: in expansion of macro ‘AMEM’
printf("%s", AMEM);

Any idea of how to put it to work?

Upvotes: 1

Views: 130

Answers (2)

mirabilos
mirabilos

Reputation: 5317

Your shell interprets (“eats up”) the double-quotes. Since they need to be part of the cpp macro (as the C compiler requires them to form a string), you must pass them to the compiler driver, which means escaping them from the shell. Try this:

gcc -D'AMEM="Deos Abencoa"' file.c

Or this (commonly seen with GNU autoconf):

gcc -DAMEM=\"Deos\ Abencoa\" file.c

Do note that there is no space after -D either.

Upvotes: 4

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

gcc -D AMEM='"Deus Abencoa"' file.c

The shell removes the single quotes, leaving the double quotes visible to the compiler. Before, the shell removed the double quotes.

Upvotes: 1

Related Questions