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

T

tropos

(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?
 
H

Howard

tropos said:
(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
 
G

GB

tropos said:
(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
 
N

None

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top