Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About __attribute__ support in different C compilers

Tags:

c

I found that a lot of compilers just do not support __attribute__.

#if defined (__GNUC__)

short xxxyyy[aaa_SIZE]__attribute__ ((aligned (32)));

#endif

or even

#ifndef __attribute__(x)

#define __attribute__(x)

#endif

As far as I know, GNUC is the only one supporting this __attribute__ — is that correct? And is it reasonable general practice to use macros like above?

Is there any C standard that supports this? Or is there any better approach?

like image 535
shinhou shu Avatar asked Sep 06 '25 08:09

shinhou shu


1 Answers

New C standard (C23 — working draft) added attributes in [[attribute]] format. It is portable. They are already a part of C++ since C++11.

Example:

[[noreturn]] void f(void) {
abort(); // ok
}

All older C standards do not have it and they are compiler extensions. Do not use them unless you really need them (for example writing low-level hardware stuff).

If such a program has to be compiled by many compilers you will need plenty #ifs.

like image 195
0___________ Avatar answered Sep 08 '25 06:09

0___________