27

Problem

I am trying to remove __attribute__ from my C code before I send it into a parser. Is there a way to define function-like macros using the -D argument?

Solution using header file

#define __attribute__(x)

Attempted Solution

gcc -E -D__attribute__(x)= testfile.c
gcc -E -D__attribute__(x) testfile.c
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stefan Bossbaly
  • 6,682
  • 9
  • 53
  • 82
  • 2
    Have you tried `gcc -E -D'__attribute__(x)' testfile.c` ? Your shell might interpret the parenthesis before sending the argument to `gcc`. – NiBZ Aug 06 '15 at 13:51
  • @NiBZ sorry that didn't worry. Came back saying "macro names must be identifiers" – Stefan Bossbaly Aug 06 '15 at 13:54
  • Wierd, it worked for me (Ubuntu 14.04) ... (see https://gist.github.com/N-Bz/dff2706c2ccec97d9542 for what I tested). If the header file solution works, then maybe the `-include` option of `gcc` (force the inclusion of a header) might help you. – NiBZ Aug 06 '15 at 14:01
  • 1
    `gcc -E -D__attribute__(x)= testfile.c` works on windows. – Jean-François Fabre Mar 27 '18 at 12:34

1 Answers1

30

from the man pages

       If you wish to define a function-like macro on the command line,
       write its argument list with surrounding parentheses before the
       equals sign (if any).  Parentheses are meaningful to most shells,
       so you will need to quote the option.  With sh and csh,
       -D'name(args...)=definition' works.

So this works on a Unix/Linux shell

gcc -D'__attribute__(x)='

on Windows CMD the original expression works, since parentheses aren't special:

gcc -D__attribute__(x)=
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30
  • Yeah, forgot the `=` in my previous comment. The `=` is needed because `-Dsomething` acts as `#define something 1`, not as `#define something`. – NiBZ Aug 06 '15 at 14:05
  • And if you have to pass it to a Makefile as a `CFLAGS=...` parameter, don't forget to quote twice, e.g. with `CFLAGS='"-D__attribute__(x)="'` (here I used double quotes inside single quotes to avoid escaping with `\ `). – Ruslan Nov 03 '20 at 23:13