Does the __attribute__
directive apply to all the members declared on one line?
int a, b, c;
Declares three int variables.
int *a, b, c;
Declares variable "a" as a pointer to int, and b and c as int.
int __attribute__((used)) a, b, c;
Does the used
attribute apply to all variables or only to a
?
The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.
The __attribute__ is added just after the variable name, and though it can appear unwieldy, it's a style you can get used to: int main(int argc __attribute__((unused)), char **argv) { ... Additional uses shown, each with a comment showing the compiler warning it might have generated.
__attribute__((section("name"))) variable attribute data and . bss . However, you might require additional data sections or you might want a variable to appear in a special section, for example, to map to special hardware. The section attribute specifies that a variable must be placed in a particular data section.
The keyword __attribute__ allows you to specify special attributes of variables or structure fields. This keyword is followed by an attribute specification inside double parentheses. Four attributes are currently defined for variables: aligned , mode , packed , and section .
From GCC: Attribute-Syntax:
An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators in a declaration of more than one identifier using a single list of specifiers and qualifiers. Such attribute specifiers apply only to the identifier before whose declarator they appear. For example, in
__attribute__((noreturn)) void d0 (void),
__attribute__((format(printf, 1, 2))) d1 (const char *, ...), d2 (void);
the
noreturn
attribute applies to all the functions declared; the format attribute only applies tod1
.
Correction: As the comment points out, my previous conclusion is incorrect. I didn't notice the other than the first part.
Modified conclusion:
In both
int __attribute__((used)) a, b, c;
and
__attribute__((used)) int a, b, c;
The attribute applies to all a, b, and c.
But if it were:
int a, __attribute__((used)) b, c;
The attribute would apply to b
only.
gcc
documentation (6.36 Attribute Syntax) says it only applies to the identifier before whose declarator they appear:
An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators in a declaration of more than one identifier using a single list of specifiers and qualifiers. Such attribute specifiers apply only to the identifier before whose declarator they appear. For example, in
__attribute__((noreturn)) void d0 (void), __attribute__((format(printf, 1, 2))) d1 (const char *, ...), d2 (void);
So in your example:
int __attribute__((used)) a, b, c;
the attribute only applies to a
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With