How to use __attribute__((used))?

Ian Lance Taylor iant@google.com
Tue Sep 17 15:08:00 GMT 2013


On Tue, Sep 17, 2013 at 7:54 AM, Paul Smith <paul@mad-scientist.net> wrote:
> I'm confused about how to use __attribute__((used)).  I'm using GCC
> 4.8.1 on a GNU/Linux Intel system.
>
> I have various variables that I want to be kept even at high levels of
> optimization, so that when I'm debugging I can use them.  I noticed the
> "used" attribute, which the docs say:
>
>         This attribute, attached to a variable, means that the variable
>         must be emitted even if it appears that the variable is not
>         referenced.
>
> That sounds like just what I want.  But whenever I try to use this I get
> an error:
>
>    error: ‘used’ attribute ignored [-Werror=attributes]
>
> I don't know what this means.  I searched but basically just found some
> other questions without answers.

In your example code you are using the attribute on a local variable.
The used attribute is only meaningful on a function or global
variable.  It's intended to be used to force the function or global
variable to be emitted even if it is not used.

It sounds like you are looking for the ability to disable optimization
for a local variable.  It doesn't make sense to say that you want to
force a local variable to exist even at high levels of optimization.
At high levels of optimization local variables more or less disappear,
though debug info is able to track some cases.  In any case, GCC has
no such ability.

Ian



More information about the Gcc-help mailing list