Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deprecate a C pre-processor macro in GCC?

I know how to use __attribute__((deprecated)) or [[deprecated]] to deprecate a function like this:

int old_fn() __attribute__ ((deprecated));
[[deprecated]] int old_fn2();

But how to deprecate a Macro like this:

#define OLD_MACRO 1
like image 840
Eric Avatar asked Apr 21 '10 08:04

Eric


People also ask

Which GCC option Undefine a preprocessor macro?

4. Which gcc option undefines a preprocessor macro? Explanation: None.

What is __ Size_type __?

Note that __SIZE_TYPE__ isn't a variable; it's a type. Compilers other than GCC probably do not provide it, unless they're trying to be compatible with GCC. If you want size_t , include <stddef. h> if you aren't including any of the other headers (such as <stdio. h> , <string.

What is __ counter __?

__COUNTER__ is a preprocessor macro available in most compilers that expands to sequential integers starting from zero. It resets at the beginning of each new translation unit (i.e. each object file), and is commonly used with the paste ( ## ) operator to create unique identifiers within other macros.

What is a GCC preprocessor?

The C preprocessor implements the macro language used to transform C, C++, and Objective-C programs before they are compiled. It can also be useful on its own.


1 Answers

Nice, elegant solution, however depending on C99 being enabled (works with gcc 4.8.2 or later, not tested on earlier versions):

#define DEPRECATED_MACRO1 _Pragma ("GCC warning \"'DEPRECATED_MACRO1' macro is deprecated\"") 7
#define DEPRECATED_MACRO2(...) _Pragma ("GCC warning \"'DEPRECATED_MACRO2' macro is deprecated\"") printf(__VA_ARGS__)

int main(int argc, char*argv[])
{
    int n = DEPRECATED_MACRO1;
    DEPRECATED_MACRO2("%d\n", n);
    return 0;
}
like image 171
Aconcagua Avatar answered Sep 19 '22 11:09

Aconcagua