This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

How to use __attribute__((used))?


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]