Fog Creek Software
Discussion Board




Avoiding unused variable warnings

I have a char[] string in each of my files produced by our source control system.
I want to avoid seeing all the unused variable warnings, but without turning off the warning for all other variables.

I could put a line like "char *p=scsid;" after the definition but this seems a bit ugly.

Is there a way to specify the variable in such a way that there is no warning?  It needs to work on C/C++ and not be compiler specific.

  Thank you

Martin Beckett
Friday, December 05, 2003

"I have a char[] string in each of my files produced by our source control system."

Why?

Almost Anonymous
Friday, December 05, 2003

He probably has it to keep the version of each of the files.

char *foo = "$Id: $";

in CVS that will put the ID of the file (version, name, path, last modified date, iirc) in that string.  Then if you need to print it out later, just print *foo.

To get rid of the warnings, comment out that line until you need it.

Andrew Hurst
Friday, December 05, 2003

Use pragmas. Insert this in your header files:

#ifdef _MSC_VER
#pragma warning(disable: 4706 4115 4100 4201 4214 4054)
#endif /* _MSC_VER */

I assume that you're using MS VisualC. Replace with or add to the 4??? numbers the code of the warning that you want to avoid.

FireMode
Friday, December 05, 2003

if you don't use the string, you could add

#if 0
char [] version = "$whatever$"
#endif

mb
Friday, December 05, 2003

It's me again, I've not seen that you asked for a compiler independant solution: I assumed also that you were using MS compiler.

Too many wrong assumptions :-)

FireMode
Friday, December 05, 2003

Thanks for the replies.

The variable is used by a test routine but because of the way the app is linked the compiler doesn't seem to know this so I can't comment it out.

The pragma directive on MSVC removes ALL unused variable warnings, on GCC you used to be able to specify a list of variables to ignore.

I have tried putting "UNUSED" in front of it, then under GCC I can define this as __attribute_unused_ to ignore it but MSVC doesn't have anything like this.

I remember seeing a pure C way round this once ehich only involved putting an empty statement in front of the declartion - I will hit google again.

Martin Beckett
Friday, December 05, 2003

gcc:
http://gcc.gnu.org/onlinedocs/gcc-3.3.2/gcc/Warning-Options.html#Warning%20Options

-Wno-unused-variable

suppresses the warning.

hoser
Friday, December 05, 2003

> The variable is used by a test routine but because of the
> way the app is linked the compiler doesn't seem to know
> this so I can't comment it out.

If the variable has external linkage then the compiler is 'wrong' about saying it is unused - at least it seems overly aggressive of it.  Which compiler is it?

Does putting an explicit 'extern' in front of the variable help?

Which MSVC you can bracket the code with

#pragma warning(disable: ...)
// code ...
#pragma warning(default: ...)

or
#pragma warning(push)
#pragma warning(disable: ...)
// code ...
#pragma warning(pop)

This limits the effect of the pragma to just the code in question ... but tends to look ugly!

Rob Walker
Friday, December 05, 2003

(void) foo;

G
Friday, December 05, 2003

You might also make the variable look like a Global variable. 

Foolish Jordan
Friday, December 05, 2003

Make it a (possibly inline) function, or a #define, something like:

inline const char *Filename() {
return "WHATEVER";
}

and so on. Just anything except for a variable, really. I've not met the compiler that will warn about unused functions, unless you have an unusably paranoid warning level set.

Insert half smiley here.
Friday, December 05, 2003

It is stunning how many bad answer to a simple question...
G got it right.

danpop
Friday, December 05, 2003

>(void) foo;

Thank you.

Martin Beckett
Friday, December 05, 2003

I probably woulda just done

if (foo == foo) {}; // kill warning

Richard P
Friday, December 05, 2003

For years I've gone with

static const char* const cvsid[] = {cvsid[1], "@(#) $Id$"};

Which is a variation on how FreeBSD (and undoubtably many other projects) does it on many of their sources.  If you allow the ID to make its way into your binaries, the 'ident' command searches for strings beginning with @(#) and lists them, giving you an exact fix on which version you're dealing with.

[excessive consts optional :]

Tom Crimi
Friday, December 05, 2003

*  Recent Topics

*  Fog Creek Home