How can I conditionally include #ident directives in a macro?
A bug in gcc-4.4 causes the #ident
directive to emit a warning. We don't allow warnings in our compiler (-Werror) so I need to turn these off when compiled on certain GCC compiler versions. (See Best replacement for GCC #ident)
$ echo '#ident "FAILS on gcc-4.3.3"' > test.c
$ gcc-4.4 -c test.c
test.c:1: warning: #ident is a deprecated GCC extension
Since these occur in several locations I want to replace them with a macro which would conditionally emit either nothing (or something that approximates #ident
) on those "bad" compilers or with the actual #ident
directive on all others. Ideally, something like this:
# test2.c
#ifndef HAS_HASH_IDENT
# define IDENT(x) //-- NO-OP
#else
# define IDENT(x) #ident x
#endif
This doesn't work because the preprocessor chokes on the #
of #ident
, as it is interpreted as the stringize operator when used in a macro.
$ gcc-4.5 -Wall -E test2.c
test2.c:4:22: error: '#' is开发者_如何学编程 not followed by a macro parameter
I tried several macro redirection tricks, but nothing I came up with would satisfy the preprocessor.
Is something like this even possible?
Note: The #ident
directive is passed on intact to the compiler by the preprocessor, so the problem I'm having is not restricted by some sort of preprocessor recursion limitation.
$ gcc-4.5 -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"
#ident "FAILS on gcc-4.3.3"
Perhaps you just want to use this option
-fno-ident
Ignore the#ident
directive.
maybe this switches off the warning, too.
Have you tried selectively muting that specific warning? Something along the lines of https://stackoverflow.com/a/3125889/2003487
精彩评论