GCC define function-like macros using -D argument

2020-04-02 16:42发布

问题:

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

回答1:

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)=