Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Initialization of static anonymous-namespace members from a dynamicallyloaded lib

Reply
Thread Tools

Re: Initialization of static anonymous-namespace members from a dynamicallyloaded lib

 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      01-09-2011
* m0shbear, on 09.01.2011 21:56:
> Given
> // foo.cpp
> #include<map>
> namespace {
> int init_s();
> std::map<int,int> m;
> int dummy = init_s();
> int init_s() {
> m[1] = 2;
> m[5] = 4;
> return 0;
> }
> };
> // ...
> int Foo::bar() {
> return m[5];
> }
> // ...
>
> // foo.h
> class Foo {
> //...
> int bar();
> // ...
> };
>
> If foo.o is linked at build time, the compiler automagically adds the
> call to init_s() in the libc++ static init. But if it is dynamically
> loaded, this condition is guaranteed to be satisfied. Or is it? I
> don't want to waste memory by having an individual copy of supposedly
> static data, yet I don't want UB from guessing and hoping.
>
> Is it even possible to have file-scope non-POD statics upon whose
> operations do not invoke UB, inside a d-l lib?


The C++ standard does not address or even acknowledge the existence of
dynamically loaded libraries except that one can interpret some wording about
delayed initialization as pertaining to dynamically loaded libraries.

Anyway, the short of it is that in Windows you can generally rely on techniques
such as above, but not in *nix.


Cheers & hth.,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      01-09-2011
On Jan 9, 9:00 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
(E-Mail Removed)> wrote:
> * m0shbear, on 09.01.2011 21:56:


[...]
> > Is it even possible to have file-scope non-POD statics upon
> > whose operations do not invoke UB, inside a d-l lib?


> The C++ standard does not address or even acknowledge the
> existence of dynamically loaded libraries except that one can
> interpret some wording about delayed initialization as
> pertaining to dynamically loaded libraries.


Yes, but most C++ compilers do permit dynamically loaded
objects. (I don't know of any that can dynamically load a
library. But despite the name, under Windows, a DLL is not a
library.)

> Anyway, the short of it is that in Windows you can generally
> rely on techniques such as above, but not in *nix.


Under which Unix does it fail? I've used the technique a lot
under various Unices, and I've never known it to fail. (I can
guarantee that it works correctly under Solaris and Linux,
because those are the Unices I currently have access to, but I
can't remember every seeing it fail.)

--
James Kanze
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      01-10-2011
* James Kanze, on 10.01.2011 00:03:
> On Jan 9, 9:00 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
> (E-Mail Removed)> wrote:
>> * m0shbear, on 09.01.2011 21:56:

>
> [...]
>>> Is it even possible to have file-scope non-POD statics upon
>>> whose operations do not invoke UB, inside a d-l lib?

>
>> The C++ standard does not address or even acknowledge the
>> existence of dynamically loaded libraries except that one can
>> interpret some wording about delayed initialization as
>> pertaining to dynamically loaded libraries.

>
> Yes, but most C++ compilers do permit dynamically loaded
> objects.


What's that "but"?

Yes, but -- I like large steaks with fries and beer!


> (I don't know of any that can dynamically load a
> library. But despite the name, under Windows, a DLL is not a
> library.)


Windows Dynamically Loaded Library, *nix Shared Library.

You mean despite the names we shouldn't talk about them as "libraries".

This terminological subtlety, where you seem to have invented your very own
terminology (confusingly using the same word as others but with a different
meaning) reminds me of a certain person that I killfiled yesterday.


>> Anyway, the short of it is that in Windows you can generally
>> rely on techniques such as above, but not in *nix.

>
> Under which Unix does it fail? I've used the technique a lot
> under various Unices, and I've never known it to fail. (I can
> guarantee that it works correctly under Solaris and Linux,
> because those are the Unices I currently have access to, but I
> can't remember every seeing it fail.)


Hm. I'm glad to hear that it works. Maybe what I thought I knew is wrong. At
least, googling "static initialization problem in shared libraries in Linux"
didn't find any of that, except some mention of problems with very old version
of g++ (2.95 or earlier) -- perhaps that's where I got wrong idea from?


Cheers,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-10-2011
On Jan 10, 12:05 am, "Alf P. Steinbach /Usenet"
<(E-Mail Removed)> wrote:
> * James Kanze, on 10.01.2011 00:03:


> > On Jan 9, 9:00 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
> > (E-Mail Removed)> wrote:
> >> * m0shbear, on 09.01.2011 21:56:


> > [...]
> >>> Is it even possible to have file-scope non-POD statics upon
> >>> whose operations do not invoke UB, inside a d-l lib?


> >> The C++ standard does not address or even acknowledge the
> >> existence of dynamically loaded libraries except that one can
> >> interpret some wording about delayed initialization as
> >> pertaining to dynamically loaded libraries.


> > Yes, but most C++ compilers do permit dynamically loaded
> > objects.


> What's that "but"?


That most C++ compilers do permit dynamically loaded objects. So
it makes sense to speak about the concept in C++. Like
threading, for example.

> > (I don't know of any that can dynamically load a
> > library. But despite the name, under Windows, a DLL is not a
> > library.)


> Windows Dynamically Loaded Library, *nix Shared Library.


What's a Unix shared library? I've never heard the term before.
(Under Unix, the files are normally called shared objects, or
simply .so files. The header of the Posix specification of
dlopen says "dlopen -- gain access to an executable object
file".)

Neither name is particularly good: they behave like object
files, not libraries, and the significant point isn't that they
are shared, but that they are dynamically loaded.

> You mean despite the names we shouldn't talk about them as
> "libraries".


Ideally, no. Practically, I think DLL is to ingrained for us to
change it. But I do constantly see people getting confused
about the issue.

> This terminological subtlety, where you seem to have invented
> your very own terminology (confusingly using the same word as
> others but with a different meaning) reminds me of a certain
> person that I killfiled yesterday.


Except that I got this point of view from discussions in the
C committee. It's certainly not my own. And calling DLL's
libraries does cause problems: we see postings all the time from
people who don't understand how libraries work, because they've
been brought up to believe that the behavior of a DLL is what
a library does.

> >> Anyway, the short of it is that in Windows you can generally
> >> rely on techniques such as above, but not in *nix.


> > Under which Unix does it fail? I've used the technique a lot
> > under various Unices, and I've never known it to fail. (I can
> > guarantee that it works correctly under Solaris and Linux,
> > because those are the Unices I currently have access to, but I
> > can't remember every seeing it fail.)


> Hm. I'm glad to hear that it works. Maybe what I thought
> I knew is wrong. At least, googling "static initialization
> problem in shared libraries in Linux" didn't find any of that,
> except some mention of problems with very old version of g++
> (2.95 or earlier) -- perhaps that's where I got wrong idea
> from?


I think that it didn't work in some very old versions of Unix
(going back 15 or 20 years or more). But they certainly work
under Solaris and Linux, and have for a long time; I'm pretty
sure the same is true for AIX and HP-UX, although it's been some
time since I've used them.

On the other hand, Posix doesn't say anything about it, at least
not that I can find. (But then, Posix doesn't say anything
about C++ in general.)

--
James Kanze
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      01-10-2011
* James Kanze, on 10.01.2011 11:26:
> On Jan 10, 12:05 am, "Alf P. Steinbach /Usenet"
> <(E-Mail Removed)> wrote:
>> * James Kanze, on 10.01.2011 00:03:

>
>>> On Jan 9, 9:00 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
>>> (E-Mail Removed)> wrote:
>>>> * m0shbear, on 09.01.2011 21:56:

>
>>> [...]
>>>>> Is it even possible to have file-scope non-POD statics upon
>>>>> whose operations do not invoke UB, inside a d-l lib?

>
>>>> The C++ standard does not address or even acknowledge the
>>>> existence of dynamically loaded libraries except that one can
>>>> interpret some wording about delayed initialization as
>>>> pertaining to dynamically loaded libraries.

>
>>> Yes, but most C++ compilers do permit dynamically loaded
>>> objects.

>
>> What's that "but"?

>
> That most C++ compilers do permit dynamically loaded objects. So
> it makes sense to speak about the concept in C++. Like
> threading, for example.


Of course it makes sense to talk about it.

That's why we're talking about it.

Exactly who do you think had said it does not make sense to talk about it?


>>> (I don't know of any that can dynamically load a
>>> library. But despite the name, under Windows, a DLL is not a
>>> library.)

>
>> Windows Dynamically Loaded Library, *nix Shared Library.

>
> What's a Unix shared library? I've never heard the term before.


Try <url: http://www.google.com/search?q=%22shared+library%22>.


> (Under Unix, the files are normally called shared objects, or
> simply .so files. The header of the Posix specification of
> dlopen says "dlopen -- gain access to an executable object
> file".)


It's a pleasure to be able to teach you something basic in a thread where you've
shown me wrong about something basic.


> Neither name is particularly good: they behave like object
> files, not libraries, and the significant point isn't that they
> are shared, but that they are dynamically loaded.
>
>> You mean despite the names we shouldn't talk about them as
>> "libraries".

>
> Ideally, no. Practically, I think DLL is to ingrained for us to
> change it. But I do constantly see people getting confused
> about the issue.
>
>> This terminological subtlety, where you seem to have invented
>> your very own terminology (confusingly using the same word as
>> others but with a different meaning) reminds me of a certain
>> person that I killfiled yesterday.

>
> Except that I got this point of view from discussions in the
> C committee. It's certainly not my own. And calling DLL's
> libraries does cause problems: we see postings all the time from
> people who don't understand how libraries work, because they've
> been brought up to believe that the behavior of a DLL is what
> a library does.


Ah, they've been doing that song-and-dance routine for you, dazzling &
misleading you.

Means you were correct about something and someone tried to wiggle out of it.

Again, try <url: http://www.google.com/search?q=%22shared+library%22>.


>>>> Anyway, the short of it is that in Windows you can generally
>>>> rely on techniques such as above, but not in *nix.

>
>>> Under which Unix does it fail? I've used the technique a lot
>>> under various Unices, and I've never known it to fail. (I can
>>> guarantee that it works correctly under Solaris and Linux,
>>> because those are the Unices I currently have access to, but I
>>> can't remember every seeing it fail.)

>
>> Hm. I'm glad to hear that it works. Maybe what I thought
>> I knew is wrong. At least, googling "static initialization
>> problem in shared libraries in Linux" didn't find any of that,
>> except some mention of problems with very old version of g++
>> (2.95 or earlier) -- perhaps that's where I got wrong idea
>> from?

>
> I think that it didn't work in some very old versions of Unix
> (going back 15 or 20 years or more). But they certainly work
> under Solaris and Linux, and have for a long time; I'm pretty
> sure the same is true for AIX and HP-UX, although it's been some
> time since I've used them.
>
> On the other hand, Posix doesn't say anything about it, at least
> not that I can find. (But then, Posix doesn't say anything
> about C++ in general.)


Cheers,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-10-2011
On Jan 10, 11:01 am, "Alf P. Steinbach /Usenet"
<(E-Mail Removed)> wrote:
> * James Kanze, on 10.01.2011 11:26:
> > On Jan 10, 12:05 am, "Alf P. Steinbach /Usenet"
> > <(E-Mail Removed)> wrote:
> >> * James Kanze, on 10.01.2011 00:03:


> >>> On Jan 9, 9:00 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
> >>> (E-Mail Removed)> wrote:
> >>>> * m0shbear, on 09.01.2011 21:56:


> >>> [...]
> >>> (I don't know of any that can dynamically load a
> >>> library. But despite the name, under Windows, a DLL is not a
> >>> library.)


> >> Windows Dynamically Loaded Library, *nix Shared Library.


> > What's a Unix shared library? I've never heard the term before.


> Try <url:http://www.google.com/search?q=%22shared+library%22>.


You can find anything using Google. It never occured to me.
I guess just basing my knowledge on the Posix standard and the
Sun manual. And conversations with other Unix specialists:
after all, .so didn't come from shared library, but from shared
object.

The fact remains that in computer science, "library" means (or
always used to mean) a collection of object files, each of which
was included only if it resolved an unresolved external. When
dynamically linking, it's all or nothing.

Anyway, trying your link, I see that it's not just newbes using
the term, even if Posix and Sun don't. The Linux and gcc
communities seem to have adopted it. Which makes me think that
I'm fighting a loosing battle for linguistic purity. The fact
remains that the behavior *is* different from that traditionally
associated with libraries, and that we do get questions (or
complaints) from people surprised that static libraries don't
behave the same as the dynamically linked stuff. (Hmm. I think
in the future, I'll just call it "dynamically linked stuff", and
avoid the issue of whether it is a library or not.)

--
James Kanze
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      01-10-2011

"James Kanze" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Jan 10, 11:01 am, "Alf P. Steinbach /Usenet"
> <(E-Mail Removed)> wrote:
>> * James Kanze, on 10.01.2011 11:26:
>> > On Jan 10, 12:05 am, "Alf P. Steinbach /Usenet"
>> > <(E-Mail Removed)> wrote:
>> >> * James Kanze, on 10.01.2011 00:03:

>
>> >>> On Jan 9, 9:00 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
>> >>> (E-Mail Removed)> wrote:
>> >>>> * m0shbear, on 09.01.2011 21:56:

>
>> >>> [...]
>> >>> (I don't know of any that can dynamically load a
>> >>> library. But despite the name, under Windows, a DLL is not a
>> >>> library.)

>
>> >> Windows Dynamically Loaded Library, *nix Shared Library.

>
>> > What's a Unix shared library? I've never heard the term before.

>
>> Try <url:http://www.google.com/search?q=%22shared+library%22>.

>
> You can find anything using Google. It never occured to me.
> I guess just basing my knowledge on the Posix standard and the
> Sun manual. And conversations with other Unix specialists:
> after all, .so didn't come from shared library, but from shared
> object.
>
> The fact remains that in computer science, "library" means (or
> always used to mean) a collection of object files, each of which
> was included only if it resolved an unresolved external. When
> dynamically linking, it's all or nothing.
>

No you are completely WRONG !
http://en.wikipedia.org/wiki/Library_(computing)


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      01-10-2011

"James Kanze" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Jan 10, 11:01 am, "Alf P. Steinbach /Usenet"
> <(E-Mail Removed)> wrote:
>> * James Kanze, on 10.01.2011 11:26:
>> > On Jan 10, 12:05 am, "Alf P. Steinbach /Usenet"
>> > <(E-Mail Removed)> wrote:
>> >> * James Kanze, on 10.01.2011 00:03:

>
>> >>> On Jan 9, 9:00 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
>> >>> (E-Mail Removed)> wrote:
>> >>>> * m0shbear, on 09.01.2011 21:56:

>
>> >>> [...]
>> >>> (I don't know of any that can dynamically load a
>> >>> library. But despite the name, under Windows, a DLL is not a
>> >>> library.)

>
>> >> Windows Dynamically Loaded Library, *nix Shared Library.

>
>> > What's a Unix shared library? I've never heard the term before.

>
>> Try <url:http://www.google.com/search?q=%22shared+library%22>.

>
> You can find anything using Google. It never occured to me.
> I guess just basing my knowledge on the Posix standard and the
> Sun manual. And conversations with other Unix specialists:
> after all, .so didn't come from shared library, but from shared
> object.
>
> The fact remains that in computer science, "library" means (or
> always used to mean) a collection of object files, each of which
> was included only if it resolved an unresolved external. When
> dynamically linking, it's all or nothing.
>

No you are completely WRONG !
http://en.wikipedia.org/wiki/Library_(computing)


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      01-10-2011

"James Kanze" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Jan 10, 11:01 am, "Alf P. Steinbach /Usenet"
> <(E-Mail Removed)> wrote:
>> * James Kanze, on 10.01.2011 11:26:
>> > On Jan 10, 12:05 am, "Alf P. Steinbach /Usenet"
>> > <(E-Mail Removed)> wrote:
>> >> * James Kanze, on 10.01.2011 00:03:

>
>> >>> On Jan 9, 9:00 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
>> >>> (E-Mail Removed)> wrote:
>> >>>> * m0shbear, on 09.01.2011 21:56:

>
>> >>> [...]
>> >>> (I don't know of any that can dynamically load a
>> >>> library. But despite the name, under Windows, a DLL is not a
>> >>> library.)

>
>> >> Windows Dynamically Loaded Library, *nix Shared Library.

>
>> > What's a Unix shared library? I've never heard the term before.

>
>> Try <url:http://www.google.com/search?q=%22shared+library%22>.

>
> You can find anything using Google. It never occured to me.
> I guess just basing my knowledge on the Posix standard and the
> Sun manual. And conversations with other Unix specialists:
> after all, .so didn't come from shared library, but from shared
> object.
>
> The fact remains that in computer science, "library" means (or
> always used to mean) a collection of object files, each of which
> was included only if it resolved an unresolved external. When
> dynamically linking, it's all or nothing.
>

No you are completely WRONG !
http://en.wikipedia.org/wiki/Library_(computing)


 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      01-11-2011
On Jan 10, 2:51*pm, James Kanze <(E-Mail Removed)> wrote:
>
> The fact remains that in computer science, "library" means (or
> always used to mean) a collection of object files, each of which
> was included only if it resolved an unresolved external. *When
> dynamically linking, it's all or nothing.


Apparently, even computer scientists are not consistent with the use
of "library".
I have a textbook here that uses the term "shared library" for those
entities that get linked into the executable at load time.
The book is
Linkers and Loaders; Academic Press; John R. Levine; 1-55860-496-0

Bart v Ingen Schenau
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cpp + Python: static data dynamic initialization in *nix shared lib? Alf P. Steinbach /Usenet Python 17 07-14-2010 10:39 AM
Cpp + Python: static data dynamic initialization in *nix shared lib? Alf P. Steinbach /Usenet C++ 10 07-13-2010 09:28 PM
any python wrapper to call .lib static library(ufmod.lib)? est Python 1 02-16-2008 10:56 AM
Perl MakeMaker - how to force Perl linking with the static C library (libcrt.lib) instead of dynamic C library (msvcrt.lib) Avi Perl Misc 0 04-17-2007 09:20 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM



Advertisments