Newer gcc swallow version control keywords

Newer gcc swallow version control keywords

Oleg Goldshmidt pub at goldshmidt.org
Mon Oct 17 18:29:58 IST 2011


Hi,

I have a gcc-related question. Problematic platform is Fedora 15 with
gcc 4.6.1, as well as Fedora 14 with gcc 4.5.1.

I am used to keeping RCS/CVS/SVN keywords (e.g., $Id$) in all my code.
In the case of C/C++ this normally amounts to

static const char foo_src_id[] = "$Id$";

in the source code. For those who have never encountered it: SVN and
friends happily substitute the relevant information for $Id$, and the
information then can be retrieved using ident(1), even from binaries,
libraries, and such - incredibly useful.

Now, put the above line in a C or C++ file, say foo.cc, and do the following:

$ g++ -g -O2 foo.cc -c -o foo.o
$ ident foo.o
foo.o:
     $Id: foo.cc 673 2011-10-17 09:48:11Z oleg $

This works up to and including gcc 4.4, but it does not work with gcc
4.5.1 or gcc 4.6.1 (the ident part does not show any keywords). The
reason seems to be that the optimizer realizes that the static const
is not used and eliminates it (remove -O2 and ident works fine).

I can fool gcc by

static const char foo_src_id[] __attribute__((used)) = "$Id$";

but this is non-portable, and preprocessor acrobatics such as

#if (__GNUC__ >= 4) && (__GNUC_MINOR__ > 4)
#define USED(x) x __attribute__((used))
#else
#define USED(x) x
#endif

static const char USED(foo_src_id[]) = "$Id$";

is incredibly ugly and verbose, IMHO (even with #ifdef __GNUC__ only).
So is any contortion to use the constant somewhere in each file.

Does anyone know of a gcc option that will keep unused static
constants even with optimization or or any other way to keep the
keywords in a portable way? I tried different things that came to mind
(such as -fkeep-static-consts) but they did not work, e.g.,
-fkeep-static-consts emits static consts only when optimization is not
on.

A bonus question: can anyone with Debian/Ubuntu/OpenSuSE/whatever
check whether it works? If it is a RedHat-specific or generic for all
GCCs is important.

-- 
Oleg Goldshmidt | pub at goldshmidt.org



More information about the Linux-il mailing list