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 const char jlss_id_errno_c[] = "@(#)$Id: errno.c,v 3.3 2011/09/07 22:33:45 jleffler Exp $"; #endif
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 extern const char jlss_id_filename_c[]; const char jlss_id_filename_c[] = "@(#)$Id$"; #endif
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 const char jlss_id_stderr_h[] = "@(#)$Id: stderr.h,v 10.3 2011/11/28 04:49:24 jleffler Exp $"; #endif #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.
#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.
Jonathan leffler
source share