jbl
jbl

Reputation: 582

Does __attribute__ apply to all the variables in a declaration?

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?

Upvotes: 33

Views: 3330

Answers (4)

Yu Hao
Yu Hao

Reputation: 122383

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 to d1.


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.

Upvotes: 26

Mark Lakata
Mark Lakata

Reputation: 20838

The answer is that the attribute probably doesn't do anything.

$ cat f.c
int foo_f1;
int __attribute__((used)) foo_f2;

main()
{
}

and

$ cat g.c
int foo_g1;
int __attribute__((used)) foo_g2;

build f as obj, g as library

$ gcc    -c -o g.o g.c
$ ar rs libg.a g.o
$ gcc -O3 f.c -lg

$ objdump.exe -t a.exe  | grep foo
[532](sec  6)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000100 _foo_f1
[599](sec  6)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000104 _foo_f2

Basically, the linker didn't remove any symbols from f.c and removed everything from g.c, even with the __attribute__((used)).

Upvotes: 1

ouah
ouah

Reputation: 145839

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.

Upvotes: 6

WedaPashi
WedaPashi

Reputation: 3872

Referring the GCC document,

The keyword __attribute__ allows you to specify special attributes when making a declaration. This keyword is followed by an attribute specification inside double parentheses. Nine attributes, noreturn, const, format, no_instrument_function, section, constructor, destructor, unused and weak are currently defined for functions. Other attributes, including section are supported for variables declarations (see section 4.29 Specifying Attributes of Variables) and for types (see section 4.30 Specifying Attributes of Types).

Section 4.29: Attributes of Variables

unused:
This attribute, attached to a variable, means that the variable is meant to be possibly unused. GNU CC will not produce a warning for this variable.

Section 4.30: Attributes of Types

unused:
When attached to a type (including a union or a struct), this attribute means that variables of that type are meant to appear possibly unused. GNU CC will not produce a warning for any variables of that type, even if the variable appears to do nothing. This is often the case with lock or thread classes, which are usually defined and then not referenced, but contain constructors and destructors that have nontrivial bookkeeping functions

Upvotes: 2

Related Questions