1

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?

1
  • Note that the space between -D and macro=value is immaterial; it works with and without that space. Commented Dec 6, 2013 at 22:14

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The space after the -D is optional and most often omitted but not necessary.
What I meant was: note that my (working) way does not have a space. I know systems that require there be no space after cpp options like -D and -I.
Which systems require 'no space'?
I saw GCC choke (on MirBSD, but I think it should also do on GNU/Linux) when the space after -I was missing. I cannot state offhead which one it was for -D as it's been a very long time since I last accidentally had a space.
I deleted my comment, as it was off-base. Please adjust your answer to say something like "your shell interprets the double quotes itself and doesn't pass them to gcc".
1
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.