We need to define a const static char
pointer in each and every header (.h) and source (.cpp) file to comply with company coding standards.
static const char * one_time_param = "ABCDEFG";
When compiled, the compiler is generating lot of "defined but not used" warnings. Does someone have a solution to this issue, please?
-Wno-unused-parameter
Using the above compiler flag, we can suppress these warnings. But, this also suppresses some other unused parameters which might need attention. We tried these solutions which only work for function parameters.
Q_UNUSED
in Qt, and
#define UNUSED(x) ((void)(x))
Previous question of similar kind:
How can I hide "defined but not used" warnings in GCC?
const char* is a pointer to a constant char, meaning the char in question can't be modified. char* const is a constant pointer to a char, meaning the char can be modified, but the pointer can not (e.g. you can't make it point somewhere else).
char* const says that the pointer can point to a char and value of char pointed by this pointer can be changed. But we cannot change the value of pointer as it is now constant and it cannot point to another char.
To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition.
So combining static and const, we can say that when a variable is initialized using static const, it will retain its value till the execution of the program and also, it will not accept any change in its value.
It's usually const pointer too in this case, so try to use:
static const char * const one_time_param = "ABCDEFG";
First - the company coding standards are arguably wasting space. If you're going to do that, then use an array instead of a char *
so you store just the data and not a pointer and the data:
static const char one_time_param[] = "ABCDEFG";
Next, presumably this is for file identification - that, at least, is what I use it for. There are several things to be aware of, learned from experience over a number of years. (I still like to embed version numbers in the source files - I haven't whole-heartedly moved to DVCS because of this.)
I'm currently using names based on 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 supported a #ident
directive:
#ident "@(#)$Id: errno.c,v 3.3 2011/09/07 22:33:45 jleffler Exp $"
The compiler included the strings in a 'comments' section in the object file, and a tool (mcs
) to manipulate the comments section (options -d
to delete it and -c
to compress it, IIRC). This section was part of the binary, but not loaded into memory at runtime.
At one point in GCC's evolution, in conjunction with the command line options I was using, I got warnings unless I declared as well as defined the variable, so my 'template' for 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 normally remove the declaration these days, and don't get compiler warnings.
As an alternative to using the file name as the basis of variable name, you could generate a UUID or GUID name in hex and use that as the variable name, with a prefix to ensure the first character is alphabetic.
In headers, you don't want that material defined in every source file that includes the header because (a) it becomes a noticable (but not necessarily significant) overhead on program size, and (b) you can't multiply define global variables (you can multiply declare them; that's not a problem). So, my headers 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 the values, I have #define MAIN_PROGRAM
at the top of the corresponding source file. For example, from running what errno
on a program of that 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
This is a complete (and very useful) program illustrating 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 that is compiled, the version string is not included in the binary (or the object file). This does not currently give me any warning when compiled 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 get no identification output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With