Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Problem: shared object loading runs constructor of a static object, but static linkage does not. (http://www.velocityreviews.com/forums/t450146-problem-shared-object-loading-runs-constructor-of-a-static-object-but-static-linkage-does-not.html)

tropos 11-29-2005 04:58 PM

Problem: shared object loading runs constructor of a static object, but static linkage does not.
 
(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;

<file main.cpp>
#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?


Howard 11-29-2005 05:07 PM

Re: Problem: shared object loading runs constructor of a static object, but static linkage does not.
 

"tropos" <tropostropos@hotmail.com> wrote in message
news:1133283504.140871.259170@g47g2000cwa.googlegr oups.com...
> (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



GB 11-30-2005 11:41 AM

Re: Problem: shared object loading runs constructor of a staticobject, but static linkage does not.
 
tropos wrote:
> (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

None 11-30-2005 04:54 PM

Re: Problem: shared object loading runs constructor of a static object, but static linkage does not.
 
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



All times are GMT. The time now is 01:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.