473,396 Members | 1,918 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,396 software developers and data experts.

Global objects in libs

Note: This might be a bit off topic..
I want to create some global objects that register themselves with another
"manager"-class upon creation:

class SomeClass { ... };

// cpp:

SomeClass globalObj;

// other code
This works fine when I just compile and link everything at the same time.
However, if I compile the stuff with global objects and manager as a
static library (*.a) on linux, and try linking it with some client code,
it doesn't seem to work.

I guess it might be
because the symbols in the lib aren't actually resolved or handled at all
when I don't reference them in my client code, but how can I make it work?
Is there another way than to put this object creation into a function?

This is all compiled with g++, lib created with 'ar'.
Hope I managed to describe my problem good enough...
Jul 22 '05 #1
1 1493
Oystein Haare wrote in news:pa****************************@online.no in
comp.lang.c++:
Note: This might be a bit off topic..
I want to create some global objects that register themselves with
another "manager"-class upon creation:

class SomeClass { ... };

// cpp:

SomeClass globalObj;

// other code
This works fine when I just compile and link everything at the same
time. However, if I compile the stuff with global objects and manager
as a static library (*.a) on linux, and try linking it with some
client code, it doesn't seem to work.

I guess it might be
because the symbols in the lib aren't actually resolved or handled at
all when I don't reference them in my client code, but how can I make
it work? Is there another way than to put this object creation into a
function?

This is all compiled with g++, lib created with 'ar'.
Hope I managed to describe my problem good enough...


Your problem is probably the way linkers work.

Usually they only link in things that are refrenced from
your non-library code.

In your library put something like:

// header code "your-lib.h"
struct link_this_library
{
static int no_use;
link_this_library()
};

// .cpp (TU) code
#include "your-lib.h"

int link_this_library::whatever = 0;
link_this_library::link_this_library()
{
++whatever;
}

// Non library code:
#include "your-lib.h"

link_this_library unused_;

int main()
{
}

The constructor call is needed as a Translation Unit (TU),
aka the code (.cpp) you put in your library, has to be
referenced so that you're guaranteed its initialization code
will be run.

unused_ will be constructed before main() is entered, so
everything should work.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

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

Similar topics

2
by: WhyteWolf | last post by:
I'm trying to set a object as global for access through out the rest of my script ... {a basic SQL accessing object} however if I try calling the object from with in another object it acts as if it...
7
by: Martin Stich | last post by:
hi, i want to redefine the global operator new for logging and stats purposes. is it a good idea to use malloc() as allocation function ? or should i call one of those weird crt functions....or...
2
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
8
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
1
by: Darin | last post by:
I have an application that is one EXE with mutliple DLL's. In one of the DLL's (libs.dll) that the EXE has refeneced, I have: Public Class Globals Public Shared gLogin as String End Class ...
23
by: David Colliver | last post by:
Hi, using c#, 1.1 I know that we are not supposed to use global variables etc. in c# I am having a problem, but not sure how to resolve. I did have another post here, but may have over...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.