This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
How to use __attribute__((used))?
- From: Paul Smith <paul at mad-scientist dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 17 Sep 2013 10:54:30 -0400
- Subject: How to use __attribute__((used))?
- Authentication-results: sourceware.org; auth=none
- Reply-to: paul at mad-scientist dot net
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