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