473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?

Nov 29 '05 #1
3 2221

"tropos" <tr**********@hotmail.com> wrote in message
news:11**********************@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
Nov 29 '05 #2
GB
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
Nov 30 '05 #3
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

Nov 30 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rickard Lind | last post by:
Is there any way to build the python executable statically and still be able to load modules built as shared libraries? I'm trying to run python scripts on a stripped down FreeBSD (4.9) machine...
1
by: Cplusplus | last post by:
as C++ defaults to external linkage for non-consts and non-static(right?): Qn) why is it then : a object declared (*without* static keyword)outside main() and outside all functions ,...
2
by: Paul Wu | last post by:
From what I understand, in ASP.NET, each HTTP requests is serviced by a separate thread. So if my code uses a static Class with shared members and properties, I can manage concurrent access by using...
3
by: jg | last post by:
Im experience some problems with static/shared classes in asp.net, Im saving some object as static so that they are allways available but the code in the static/shared constructors doesnt seem to...
15
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
15
by: Bit byte | last post by:
I am writing a small parser object. I need to store keywords etc in lsts. Because this data is to be shared by all instances of my parser class, I have declared the variable as class variables...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.