ron
ron

Reputation: 995

gcc compiler option to warn on unused global variables defined outside of main in C

# include <stdio.h>
# include <stdlib.h>

int myvariable_NOT_caught_by_Wall;

int main ( int argc, char *argv[] )
{
   int myvariable_caught_by_Wall;

   return 0;
}

is it normal for gcc when compiling via gcc some_program.c -Wall for no warning to be issued for unused global variables?

Is there a way to make that happen? I've experienced this using gcc-4.3.4 and gcc-4.8.3 in SLES_11.4

Upvotes: 1

Views: 2062

Answers (2)

ron
ron

Reputation: 995

thanks to the guy who said static that does what I am specifically looking for. I do not want to use other compile options besides -O2 -Wall if I don't have to.

and my goal is to simply have one .c file written, and I need to write it quickly, to do a particular data processing task, where it's not worth the effort to do extra writing of code using malloc() and calloc() and free() when I can simply do a global outside of main() such as double myarray[5][2][10][3600][9000][2];

if you are wondering, it's for storing some signal data dumped to a text file, I can have up to 9000 samples or more for each look angle where it's

[#elevation_angles] [polarization h or v] [#frequencies] [#az_angles][#samples] [i or q]

# include <stdio.h>
# include <stdlib.h>

static int myvariable_NOW_caught_by_Wall;  /* -Wall catches this when static */

int main ( int argc, char *argv[] )
{
   int myvariable_caught_by_Wall;

   return 0;
}

Upvotes: 0

R Sahu
R Sahu

Reputation: 206717

is it normal for gcc when compiling via gcc some_program.c -Wall for no warning to be issued for unused global variables?

Yes.

The compiler cannot know at compile time that the global variable is not used.

Imagine another .c file:

extern int myvariable_NOT_caught_by_Wall;

void foo()
{
   if (myvariable_NOT_caught_by_Wall == 0 )
   {
      dothis();
   }
   else
   {
      dothat();
   }
}

Upvotes: 3

Related Questions