Portál AbcLinuxu, 18. prosince 2025 14:18
rastos:/tmp$ cat unusedtest.c static const int rastos=0 attribute((unused));a chcem, aby ju (a len ju!)
gcc -Wall ignoroval. Tak som použil attribute unused, ale dostanem:
rastos:/tmp$ gcc -Wall -c unusedtest.c
unusedtest.c:1:27: error: expected ‘,’ or ‘;’ before ‘attribute’
static const int rastos=0 attribute((unused));
^~~~~~~~~
unusedtest.c:1:18: warning: ‘rastos’ defined but not used [-Wunused-const-variable=]
static const int rastos=0 attribute((unused));
^~~~~~
rastos:/tmp$ gcc --version
gcc (GCC) 7.2.0
Čo je zle?
Řešení dotazu:
static const int rastos __attribute__((unused)) = 0;
$ cat unusedtest.c static const int rastos __attribute__((unused)) = 0;
__attribute__ (ne attribute), a dále atribut patří za proměnnou nebo před ni, dokonce může být i mezi typem a jménem proměnné, ale ne až za inicializací. Tohle mi projde:
int f()
{
static const int foo1 __attribute__((unused)) = 1;
__attribute__((unused)) static const int foo2 = 1;
static const int __attribute__((unused)) foo3 = 1;
return 1;
}
(zkoušeno s gcc 4.8, nemám teď po ruce stroj s gcc7)
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.