<div dir="ltr"><div>I guess that it doesn't apply to libraries, which must include this global variable.</div><div><br></div>Excuse the idiotic solution, but can't you just add an option to print it out?<div><br></div>
<div>int main(int argc,char**argv) {if (argc == 2 && strcmp(argv[1],"--ident") puts(ident);...}</div><div><br></div><div>The only trick the optimizer can play here, is inlining puts and the input string to assembly commands, but I really don't think it can happen in practice.</div>
<div><br></div><div>It seems like a good idea anyhow, to have a stable way of extracting this ident string from the executable (what happens for instance if by accident you have rcsident and rcs_ident? How would you know from the stripped executable which one to trust?).<br>
<br><div class="gmail_quote">On Mon, Oct 17, 2011 at 6:29 PM, Oleg Goldshmidt <span dir="ltr"><<a href="mailto:pub@goldshmidt.org">pub@goldshmidt.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi,<br>
<br>
I have a gcc-related question. Problematic platform is Fedora 15 with<br>
gcc 4.6.1, as well as Fedora 14 with gcc 4.5.1.<br>
<br>
I am used to keeping RCS/CVS/SVN keywords (e.g., $Id$) in all my code.<br>
In the case of C/C++ this normally amounts to<br>
<br>
static const char foo_src_id[] = "$Id$";<br>
<br>
in the source code. For those who have never encountered it: SVN and<br>
friends happily substitute the relevant information for $Id$, and the<br>
information then can be retrieved using ident(1), even from binaries,<br>
libraries, and such - incredibly useful.<br>
<br>
Now, put the above line in a C or C++ file, say foo.cc, and do the following:<br>
<br>
$ g++ -g -O2 foo.cc -c -o foo.o<br>
$ ident foo.o<br>
foo.o:<br>
$Id: foo.cc 673 2011-10-17 09:48:11Z oleg $<br>
<br>
This works up to and including gcc 4.4, but it does not work with gcc<br>
4.5.1 or gcc 4.6.1 (the ident part does not show any keywords). The<br>
reason seems to be that the optimizer realizes that the static const<br>
is not used and eliminates it (remove -O2 and ident works fine).<br>
<br>
I can fool gcc by<br>
<br>
static const char foo_src_id[] __attribute__((used)) = "$Id$";<br>
<br>
but this is non-portable, and preprocessor acrobatics such as<br>
<br>
#if (__GNUC__ >= 4) && (__GNUC_MINOR__ > 4)<br>
#define USED(x) x __attribute__((used))<br>
#else<br>
#define USED(x) x<br>
#endif<br>
<br>
static const char USED(foo_src_id[]) = "$Id$";<br>
<br>
is incredibly ugly and verbose, IMHO (even with #ifdef __GNUC__ only).<br>
So is any contortion to use the constant somewhere in each file.<br>
<br>
Does anyone know of a gcc option that will keep unused static<br>
constants even with optimization or or any other way to keep the<br>
keywords in a portable way? I tried different things that came to mind<br>
(such as -fkeep-static-consts) but they did not work, e.g.,<br>
-fkeep-static-consts emits static consts only when optimization is not<br>
on.<br>
<br>
A bonus question: can anyone with Debian/Ubuntu/OpenSuSE/whatever<br>
check whether it works? If it is a RedHat-specific or generic for all<br>
GCCs is important.<br>
<font color="#888888"><br>
--<br>
Oleg Goldshmidt | <a href="mailto:pub@goldshmidt.org">pub@goldshmidt.org</a><br>
<br>
_______________________________________________<br>
Linux-il mailing list<br>
<a href="mailto:Linux-il@cs.huji.ac.il">Linux-il@cs.huji.ac.il</a><br>
<a href="http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il" target="_blank">http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il</a><br>
</font></blockquote></div><br></div></div>