The official documentation here only lists the minimum 开发者_开发技巧required version for a very small number of attributes:
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Is there a complete list of which version each attribute was added in? Even better would be a list that also shows which ones are compatible with pcc and tcc.
For gcc versions:
http://www.ohse.de/uwe/articles/gcc-attributes.html
For pcc:
http://pcc.ludd.ltu.se/fisheye/browse/~raw,r=HEAD/pcc/pcc/cc/ccom/gcc_compat.c
For tcc:
??
For a lot of the useful ones you can copy the gcc version tests from glib's gmacros.h: http://git.gnome.org/browse/glib/tree/glib/gmacros.h
Depending on your project you may also be able to just use GLib, and then use G_GNUC_NORETURN or whatever instead of the __attribute__
directly.
It probably would be better in principle to do HAVE_GCC_ATTRIBUTE_NORETURN as Juliano suggests, but it may also be YAGNI work, depending on your project.
I found this link which is slightly newer than the accepted answer: https://patchwork.ozlabs.org/project/gcc/patch/20170706132518.GO3988@redhat.com/
Apparently up to 8.1.0. Not sure it is complete though.
Do yourself a favor and don't do that.
Perhaps your software has some pre-compilation configuration/setup phase (like the autoconf team... ugh! (shivers) or something more sane like CMake), use that.
Write small tests (autoconf macros) that check if the compiler accepts the __attribute__
you are interested in and if it works as expected. If successful, write a HAVE_GCC_ATTRIBUTE_XXX
macro in a config.h
file which is included from your source.
Then use that macro with #ifdef
to test if you should put or not the attribute in the function, or perhaps if you should use another hack to emulate the lack of the attribute.
精彩评论