How to use __attribute__((used))?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

Here's my sample.  I've tried moving the attribute around but no joy:


        $ cat /tmp/unused.c
        #include <string.h>
        #include <stdio.h>
        
        void foo(const char *bar)
        {
            unsigned int len __attribute__((used)) = strlen(bar);
            printf("bar = %s\n", bar);
        }
        
        $ gcc -Wall -Werror -o /tmp/unused.o -c /tmp/unused.c
        /tmp/unused.c: In function 'foo':
        /tmp/unused.c:6:5: error: 'used' attribute ignored [-Werror=attributes]
             unsigned int len __attribute__((used)) = strlen(bar);
             ^
        /tmp/unused.c:6:18: error: unused variable 'len' [-Werror=unused-variable]
             unsigned int len __attribute__((used)) = strlen(bar);
                          ^
        cc1: all warnings being treated as errors
        





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux