C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem: shared object loading runs constructor of a static

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
tropos
Guest





PostPosted: Tue Nov 29, 2005 4:58 pm    Post subject: Problem: shared object loading runs constructor of a static Reply with quote



(Platform: Solaris with gmake and native Sun C++ compiler)

Problem: If I create a shared object (.so file) and load it into a
executable, the loader correctly runs constructors of static objects in
the .so file. But if I link the same code statically, with no shared
object, then the constructors don't run at all! Why??

Here's an example:

<file AnnounceConstruction.cpp>

#include <iostream> //for cout
#include <ostream> //needed for cout linkage

class AnnounceConstruction
{
public:
AnnounceConstruction()
{ std::cout << "AnnounceConstruction constructed."
<< std::endl; }
};

//The static object
AnnounceConstruction _static_AnnounceConstruction_instance;

#include <iostream> //for cout
#include <ostream> //needed for cout linkage

int main()
{
std::cout << "main() has run." << std::endl;
}

# Now I compile AnnounceConstruction.cpp into an archive, link
statically,
# and show that the constructor of AnnounceConstruction does not run.

staticdynamic> CC -c AnnounceConstruction.cpp -o
obj/AnnounceConstruction.o
staticdynamic> CC -xar -o AnnounceConstruction.a
obj/AnnounceConstruction.o
staticdynamic> CC main.cpp -Bstatic AnnounceConstruction.a -o test.out
staticdynamic> ./test.out
main() has run.
staticdynamic>

# This time, I compile AnnounceConstruction.cpp into a shared object,
link,
# and show that the constructor runs correctly.
staticdynamic> CC -G obj/AnnounceConstruction.o -o
AnnounceConstruction.so
staticdynamic> CC main.cpp -L. -Bdynamic AnnounceConstruction.so -o
test.out
staticdynamic> export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
staticdynamic> ./test.out
AnnounceConstruction constructed.
main() has run.
staticdynamic>

Can anyone explain this?

Back to top
Howard
Guest





PostPosted: Tue Nov 29, 2005 5:07 pm    Post subject: Re: Problem: shared object loading runs constructor of a st Reply with quote




"tropos" <tropostropos (AT) hotmail (DOT) com> wrote

Quote:
(Platform: Solaris with gmake and native Sun C++ compiler)

Problem: If I create a shared object (.so file) and load it into a
executable, the loader correctly runs constructors of static objects in
the .so file. But if I link the same code statically, with no shared
object, then the constructors don't run at all! Why??


Linking and loading are not issues covered by the C++ language standard, and
thus are off-topic here. You need to ask in a newsgroup devoted to your
platform.


-Howard



Back to top
GB
Guest





PostPosted: Wed Nov 30, 2005 11:41 am    Post subject: Re: Problem: shared object loading runs constructor of a st Reply with quote



tropos wrote:
Quote:
(Platform: Solaris with gmake and native Sun C++ compiler)

Problem: If I create a shared object (.so file) and load it into a
executable, the loader correctly runs constructors of static objects in
the .so file. But if I link the same code statically, with no shared
object, then the constructors don't run at all! Why??

Here's an example:


staticdynamic> CC -c AnnounceConstruction.cpp -o
obj/AnnounceConstruction.o
staticdynamic> CC -xar -o AnnounceConstruction.a
obj/AnnounceConstruction.o
staticdynamic> CC main.cpp -Bstatic AnnounceConstruction.a -o test.out
staticdynamic> ./test.out
main() has run.
staticdynamic


Your AnnnounceConstruction.o file is not actually getting pulled in from
the .a file because main.o has no dependencies on it. That's the whole
point of ar files. You only get what you use. Try compiling by explicity
listing the .o file on the CC line instead of creating an ar file.

Gregg

Back to top
None
Guest





PostPosted: Wed Nov 30, 2005 4:54 pm    Post subject: Re: Problem: shared object loading runs constructor of a sta Reply with quote

Another option to solve your problem is to pass some command line args
to the linker to tell it not to be so smart about deciding which
symbols to import or not. This varies tremendously by platform - for
example, using gnu ld, you would use the option --whole-archive (I
think - it's been a while since I've done this. The man page gives a
good explanation of how to use it.

-matt

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.