473,624 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 AnnounceConstru ction.cpp>

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

class AnnounceConstru ction
{
public:
AnnounceConstru ction()
{ std::cout << "AnnounceConstr uction constructed."
<< std::endl; }
};

//The static object
AnnounceConstru ction _static_Announc eConstruction_i nstance;

<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 AnnounceConstru ction.cpp into an archive, link
statically,
# and show that the constructor of AnnounceConstru ction does not run.

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

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

Can anyone explain this?

Nov 29 '05 #1
3 2244

"tropos" <tr**********@h otmail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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 AnnounceConstru ction.cpp -o
obj/AnnounceConstru ction.o
staticdynamic> CC -xar -o AnnounceConstru ction.a
obj/AnnounceConstru ction.o
staticdynamic> CC main.cpp -Bstatic AnnounceConstru ction.a -o test.out
staticdynamic> ./test.out
main() has run.
staticdynamic>


Your AnnnounceConstr uction.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
2600
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 which has no shared system libraries so I want to link it statically against libc et al, but it would be nice to still be able to load modules which were built as shared libraries. In particular I have a library for which I've generated a wrapper...
1
1555
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 , automatically considered a static object ?. doesn't static mean its internal linkage? accding me if u define a object like this then A should be external linkage. Obj A('a'); // Global int main()
2
3738
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 something like the Monitor or ReaderWriterLock class. It is rather difficult to simulate multiple threads in a debug environment so I am hoping someone can enlighten me by telling me what happens in the following scenario? A request is made to...
3
1042
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 execute??
15
4923
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 String ' this is ineffect a global application object End Module
15
3555
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 (i.e. statics). //declarations in parser class (private section) static list<stringm_keywords, m_symbols_used; static map<string, myParser::FuncDatam_mapped_funcs ; I have initialization code like this:
36
3826
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 program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
6
3181
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 singleton is initialized as a static variable , it seems there is some threading issue . Is it the issue during singleton initialization only , or during the access also? If the singleton is per thread basis (then no more singleton though ), and...
13
2459
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 certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
0
8233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8334
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.