Volatile

81 views
Skip to first unread message

Rick

unread,
Jan 13, 2009, 7:15:32 PM1/13/09
to
My coworkers tell me that there is no way to put a string in a
library, link with the library statically, and be able to use the
command line utility strings to find the string in the executable
application. The goal is to be able to say definitively what version
of the library was used.

I said volatile did that; they said it didn't. I say if you use the
volatile keyword, you have a promise that the compiler won't discard
the parcel of memory, even if it is never used for anything. They say
if you type in volatile const char version_string[] = "This is library
StevesLibrary, version 1.23.1."; the compiler or linker optimizes it
out.

What does the standard say?


Regards,

Rick

--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Jack Klein

unread,
Jan 14, 2009, 12:05:01 AM1/14/09
to
On Tue, 13 Jan 2009 18:15:32 CST, Rick <Cub...@gmail.com> wrote in
comp.std.c++:

> My coworkers tell me that there is no way to put a string in a
> library, link with the library statically, and be able to use the
> command line utility strings to find the string in the executable
> application. The goal is to be able to say definitively what version
> of the library was used.
>
> I said volatile did that; they said it didn't. I say if you use the
> volatile keyword, you have a promise that the compiler won't discard
> the parcel of memory, even if it is never used for anything. They say
> if you type in volatile const char version_string[] = "This is library
> StevesLibrary, version 1.23.1."; the compiler or linker optimizes it
> out.
>
> What does the standard say?

The C++ standard says that a namespace scope object has internal
linkage in C++, which is a change from the default external linkage in
C.

So given your:

volatile const char version_string[] = "This is library StevesLibrary,
version 1.23.1."

....if the compiler can determine that you do not access or take the
address of the character array in the translation unit, it is
perfectly acceptable for it to omit it.

On the other hand, if you override the default internal linkage with:

extern volatile const char version_string [] = /* ... */;

....than the compiler will most likely have to include it in the object
file for that translation unit. Depending on the tool set, the linker
may detect that it is never actually referenced and strip it from the
final executable. Since it is never actually accessed in the program,
no strictly conforming program could tell that it was removed.

The volatile keyword does not, nor is it intended to, guarantee the
presence of anything. Its purpose is to prevent the compiler from
performing certain optimizations on accessing (reading or writing) the
value of the object. When there are no accesses, the keyword is
meaningless.

Your tool set may have a particular option to cause it to retain such
strings. If that is of interest to you, consult the compiler's
documentation or ask in a compiler specific support group.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

James Kanze

unread,
Jan 14, 2009, 11:38:35 AM1/14/09
to
On Jan 14, 1:15 am, Rick <Cubs...@gmail.com> wrote:
> My coworkers tell me that there is no way to put a string in a
> library, link with the library statically, and be able to use
> the command line utility strings to find the string in the
> executable application. The goal is to be able to say
> definitively what version of the library was used.

> I said volatile did that; they said it didn't. I say if you
> use the volatile keyword, you have a promise that the compiler
> won't discard the parcel of memory, even if it is never used
> for anything. They say if you type in volatile const char
> version_string[] = "This is library StevesLibrary, version
> 1.23.1."; the compiler or linker optimizes it out.

> What does the standard say?

Nothing about what a utility like strings might or might not
see. Everything in this regard is implementation defined.

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

unread,
Jan 14, 2009, 11:43:38 AM1/14/09
to
On Jan 14, 6:05 am, Jack Klein <jackkl...@spamcop.net> wrote:
> On Tue, 13 Jan 2009 18:15:32 CST, Rick <Cubs...@gmail.com> wrote in
> comp.std.c++:

> > My coworkers tell me that there is no way to put a string in
> > a library, link with the library statically, and be able to
> > use the command line utility strings to find the string in
> > the executable application. The goal is to be able to say
> > definitively what version of the library was used.

> > I said volatile did that; they said it didn't. I say if you
> > use the volatile keyword, you have a promise that the
> > compiler won't discard the parcel of memory, even if it is
> > never used for anything. They say if you type in volatile
> > const char version_string[] = "This is library
> > StevesLibrary, version 1.23.1."; the compiler or linker
> > optimizes it out.

> > What does the standard say?

> The C++ standard says that a namespace scope object has
> internal linkage in C++, which is a change from the default
> external linkage in C.

Only if it is const, and if the definition doesn't specify
otherwise.

> So given your:

> volatile const char version_string[] = "This is library StevesLibrary,
> version 1.23.1."

> ....if the compiler can determine that you do not access or
> take the address of the character array in the translation
> unit, it is perfectly acceptable for it to omit it.

Even if you do access it or take its address, the compiler is
free to omit it if it can prove that the observed behavior of
the program will still be the same. (Admittedly, taking the
address makes this significantly more difficult.)

> On the other hand, if you override the default internal
> linkage with:

> extern volatile const char version_string [] = /* ... */;

> ....than the compiler will most likely have to include it in
> the object file for that translation unit.

Unless it's doing inter-module optimization, and detects that it
isn't used. Still, it's a good idea; it will probably work in
almost all implementations. (As you say later, the use of
volatile here is, of course, irrelevant.)

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


--

Francis Glassborow

unread,
Jan 14, 2009, 11:44:24 AM1/14/09
to
Rick wrote:
> My coworkers tell me that there is no way to put a string in a
> library, link with the library statically, and be able to use the
> command line utility strings to find the string in the executable
> application. The goal is to be able to say definitively what version
> of the library was used.
>
> I said volatile did that; they said it didn't. I say if you use the
> volatile keyword, you have a promise that the compiler won't discard
> the parcel of memory, even if it is never used for anything. They say
> if you type in volatile const char version_string[] = "This is library
> StevesLibrary, version 1.23.1."; the compiler or linker optimizes it
> out.
>
> What does the standard say?

As leaving it out would result in no change to the observable behaviour
of the program, the implementation is free to leave it out. In order to
ensure that it is included you need to use it in some way.

Rick

unread,
Jan 14, 2009, 1:07:42 PM1/14/09
to
On Jan 13, 11:05 pm, Jack Klein <jackkl...@spamcop.net> wrote:
> On Tue, 13 Jan 2009 18:15:32 CST, Rick <Cubs...@gmail.com> wrote in
> alt.comp.lang.learn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

>
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-...@netlab.cs.rpi.edu]

> [ --- Please see the FAQ before posting. --- ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html ]

Thanks, Jack.

I didn't know that globals by default have internal linkage in C++.
I've been working with C++ for years now, and I still get surprised.

I will ask in the appropriate newsgroup, when I can figure out what
that is, but the toolset I'm using is so common, I bet somebody just
knows the answer. I am using g++. In fact, as you suggest, with the
extern keyword, strings on the static library finds the string.
Still, the final application, after linking, doesn't have the string.
I have looked through the gnu ld documentation, and can't seem to find
an appropriate setting to prevent the linker from discarding the
symbol. Does anyone just happen to know which is the appropriate
option?

Regards,

Rick

--

Jack Klein

unread,
Jan 14, 2009, 7:20:49 PM1/14/09
to
On Wed, 14 Jan 2009 10:43:38 CST, James Kanze <james...@gmail.com>
wrote in comp.std.c++:

> On Jan 14, 6:05 am, Jack Klein <jackkl...@spamcop.net> wrote:
> > On Tue, 13 Jan 2009 18:15:32 CST, Rick <Cubs...@gmail.com> wrote in
> > comp.std.c++:
>
> > > My coworkers tell me that there is no way to put a string in
> > > a library, link with the library statically, and be able to
> > > use the command line utility strings to find the string in
> > > the executable application. The goal is to be able to say
> > > definitively what version of the library was used.
>
> > > I said volatile did that; they said it didn't. I say if you
> > > use the volatile keyword, you have a promise that the
> > > compiler won't discard the parcel of memory, even if it is
> > > never used for anything. They say if you type in volatile
> > > const char version_string[] = "This is library
> > > StevesLibrary, version 1.23.1."; the compiler or linker
> > > optimizes it out.
>
> > > What does the standard say?
>
> > The C++ standard says that a namespace scope object has
> > internal linkage in C++, which is a change from the default
> > external linkage in C.
>
> Only if it is const, and if the definition doesn't specify
> otherwise.

Yes, thanks for catching the omission of const in my post.

[ comp.std.c++ is moderated. To submit articles, try just posting with ]

Greg Herlihy

unread,
Jan 14, 2009, 7:22:54 PM1/14/09
to
On Jan 14, 10:07 am, Rick <Cubs...@gmail.com> wrote:
> On Jan 13, 11:05 pm, Jack Klein <jackkl...@spamcop.net> wrote:
>
> > > What does the standard say?
>
> > The C++ standard says that a namespace scope object has internal
> > linkage in C++, which is a change from the default external linkage in
> > C.
>
> I didn't know that globals by default have internal linkage in C++.
> I've been working with C++ for years now, and I still get surprised.

Just to clarify: - const - global objects have internal linkage by
default. Otherwise, non-const global objects have external linkage by
default. See §3.5/4.

Greg

Jack Klein

unread,
Jan 14, 2009, 7:21:15 PM1/14/09
to
On Wed, 14 Jan 2009 12:07:42 CST, Rick <Cub...@gmail.com> wrote in
comp.std.c++:


> Thanks, Jack.
>
> I didn't know that globals by default have internal linkage in C++.
> I've been working with C++ for years now, and I still get surprised.
>
> I will ask in the appropriate newsgroup, when I can figure out what
> that is, but the toolset I'm using is so common, I bet somebody just
> knows the answer. I am using g++. In fact, as you suggest, with the
> extern keyword, strings on the static library finds the string.
> Still, the final application, after linking, doesn't have the string.
> I have looked through the gnu ld documentation, and can't seem to find
> an appropriate setting to prevent the linker from discarding the
> symbol. Does anyone just happen to know which is the appropriate
> option?
>
> Regards,
>
> Rick

As James pointed out, I omitted mentioning the const keyword in my
post. Namespace scope const objects have internal linkage by default,
without the const qualifier, they have external linkage just as in C.

Have you thought of the gnu.* family of groups?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for

comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++

http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

[ comp.std.c++ is moderated. To submit articles, try just posting with ]

Reply all
Reply to author
Forward
0 new messages