Static const char * - defined but not used

We need to define a const static char pointer in each header (.h) and source (.cpp) file to comply with company coding standards.

 static const char * one_time_param = "ABCDEFG"; 

When compiled, the compiler generates many “specific but not used” warnings. Does anyone have a solution to this problem, please?

 -Wno-unused-parameter 

Using the aforementioned compiler flag, we can suppress these warnings. But it also suppresses some other unused options that may require attention. We tried these solutions, which only work for function parameters.

 Q_UNUSED 

in Qt and

 #define UNUSED(x) ((void)(x)) 

A previous question of this kind:

How can I hide “specific but not used” warnings in GCC?

+7
source share
8 answers

First, company coding standards may be losing space. If you are going to do this, use an array instead of char * to store only data, not a pointer and data:

 static const char one_time_param[] = "ABCDEFG"; 

Further, presumably this is for identifying the file - this is at least what I use it for. There are several things you need to know about, learned from experience over the years. (I still love to embed version numbers in the source files - because of this, I did not wholeheartedly switch to DVCS.)

  • To avoid warnings, you must make the characters visible outside the file.
  • This, in turn, means that you must make the variable names unique.
  • I am currently using names based on the file name: jlss_id_filename_c[] , etc.

     #ifndef lint /* Prevent over-aggressive optimizers from eliminating ID string */ const char jlss_id_errno_c[] = "@(#)$Id: errno.c,v 3.3 2011/09/07 22:33:45 jleffler Exp $"; #endif /* lint */ 
  • The AT & T SVR4 C compiler and support software supports the #ident directive:

     #ident "@(#)$Id: errno.c,v 3.3 2011/09/07 22:33:45 jleffler Exp $" 

    The compiler included lines in the comments section of the object file and a tool ( mcs ) for managing the comments section (options -d to remove it, and -c to compress it, IIRC). This section was part of the binary, but it didn’t load into memory at runtime.

  • At some point in the GCC evolution, combined with the command line options that I used, I received warnings if I did not declare and also did not define a variable, so my "template" for a new source file generates:

     #ifndef lint /* Prevent over-aggressive optimizers from eliminating ID string */ extern const char jlss_id_filename_c[]; const char jlss_id_filename_c[] = "@(#)$Id$"; #endif /* lint */ 

    However, I usually delete the declaration these days and do not get a compiler warning.

  • As an alternative to using the file name as the basis for the variable name, you can generate a UUID or GUID in hexadecimal format and use it as a variable name with a prefix so that the first character is alphabetic.

  • In the headers, you don’t want this material to be defined in every source file that includes the header, because (a) it becomes noticeable (but not necessarily significant) overhead by the size of the program, and (b) you can’t multiply the definition of global variables (you can propagate them, this is not a problem). So my headings have a stanza like:

     #ifdef MAIN_PROGRAM #ifndef lint /* Prevent over-aggressive optimizers from eliminating ID string */ const char jlss_id_stderr_h[] = "@(#)$Id: stderr.h,v 10.3 2011/11/28 04:49:24 jleffler Exp $"; #endif /* lint */ #endif 

    Then, when I want the headers to define values, I have #define MAIN_PROGRAM at the top of the corresponding source file. For example, from running what errno in a program of this name, I get the output:

     errno: $Id: errno.c,v 3.3 2011/09/07 22:33:45 jleffler Exp $ $Id: range.h,v 1.8 2008/02/11 07:39:36 jleffler Exp $ $Id: stderr.h,v 10.3 2011/11/28 04:49:24 jleffler Exp $ $Id: errhelp.c,v 8.5 2009/03/02 19:13:51 jleffler Exp $ $Id: range2.c,v 1.8 2008/02/11 08:44:50 jleffler Exp $ $Id: stderr.c,v 10.7 2011/11/28 04:49:24 jleffler Exp $ stderr.c configured with USE_STDERR_FILEDESC stderr.c configured with USE_STDERR_SYSLOG 

old style

This is a complete (and very useful) program that illustrates the old style of doing business.

 /* @(#)File: $RCSfile: al.c,v $ @(#)Version: $Revision: 1.4 $ @(#)Last changed: $Date: 1996/08/13 11:14:15 $ @(#)Purpose: List arguments one per line @(#)Author: J Leffler @(#)Copyright: (C) JLSS 1992,1996 @(#)Product: :PRODUCT: */ /*TABSTOP=4*/ #include <stdio.h> #include <stdlib.h> #ifndef lint static const char sccs[] = "@(#)$Id: al.c,v 1.4 1996/08/13 11:14:15 johnl Exp $"; #endif int main(int argc, char **argv) { while (*++argv) puts(*argv); return(EXIT_SUCCESS); } 

NB: when this is compiled, the version string is not included in the binary (or object file). This currently does not give me a warning when compiling with GCC 4.6.1 compiled on MacOS X 10.7.2:

 gcc -m64 -g -O -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual \ -Wstrict-prototypes -Wmissing-prototypes -o al al.c 

When I run what al , I do not get identification output.

+11
source

It is usually a const pointer in this case, so try using:

 static const char * const one_time_param = "ABCDEFG"; 
+17
source

Take a look at __attribute__((used)) .

+4
source

Will the variable ever refer to some external utility that checks the executable, or is that what you should have in the source?

If you just have to have it in the source, and it doesn't have to be in the compiled executable, why not #if it:

 #if 0 static const char * one_time_param = "ABCDEFG"; #endif 

An additional advantage of this method is that you no longer need to worry about name collisions in header files.

+1
source

You can always hack it. for example if (one_time_param[0] == one_time_param[0]); Minimal computational effort, and it should remove the warning. It is possible that this line will be optimized for no-op, because it is essentially useless for the program.

It depends on how elegant you want the solution. Perhaps someone can recommend a compiler flag that will get rid of the warning.

0
source

Define this in one heading, define one inline function inside the heading to “get” the value of the pointer, and then include that heading wherever you need the definition.

0
source

In the global header for your project, declare a macro as:

 #define DECLARE_ONETIME_CONST(name,value) \ static const char* name = (value); \ static const char nowarning_##name = name[0]; 

Then in your files say:

 DECLARE_ONETIME_CONST(one_time_param, "ABCDEFG"); 

It seems to need work. No matter what you might think of macros and inserting tokens, at least with a macro, you can find these things and get rid of them when people realize that they are stupid. <& shakes GT;

0
source

one_time_param.h

 #ifndef ONE_TIME_PARAM_H #define ONE_TIME_PARAM_H extern const char* one_time_param; #endif 

one_time_param.cpp

 #include "one_time_param.h" const char* one_time_param = "ABCDEFG"; 

Then include one_time_param.h in each header and source file.

& scrathes head> this, of course, will not work if you need static .

0
source

All Articles