Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC define function-like macros using -D argument

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
like image 877
Stefan Bossbaly Avatar asked Aug 06 '15 13:08

Stefan Bossbaly


People also ask

How do you define a function-like a macro?

Function-like macro definition: An identifier followed by a parameter list in parentheses and the replacement tokens. The parameters are imbedded in the replacement code. White space cannot separate the identifier (which is the name of the macro) and the left parenthesis of the parameter list.

Can I #define in a macro?

(2) However, you cannot define a macro of a macro like #define INCLUDE #define STDH include <stdio. h> .

Can we pass arguments in macro?

Function-like macros can take arguments, just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace.

Which of the following directive creates functions like macros?

The first form of the #define directive is called an object-like macro. The second form is called a function-like macro. This directive cancels a previous definition of the identifier by #define .


1 Answers

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)=
like image 135
Robert Jacobs Avatar answered Sep 20 '22 18:09

Robert Jacobs