473,404 Members | 2,170 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,404 software developers and data experts.

Linker dropping objects from static library problem

In his book, "Modern C++ Design", Andrei Alexandrescu presents a Generic
Object Factory class (Loki::Factory). One of its advantages (also mentioned
in
the book) is being able to register the class in the factory without having
a
central place that needs to be modified each time we add a new type. For
example, the registration can be done in the same compilation unit as the
class implementation:

(code like this has been presented in the book)

// DerivedType.cpp
// DerivedType is the type we want to register, and it is derived from
BaseType
namespace {
BaseType *CreateDerivedTypeObject() { return new DerivedType; }
const bool registered = MyFactory::Instance().Register("DerivedType",
CreateDerivedTypeObject);
}

MyFactory is:
typedef Loki::SingletonHolder<Loki::Factory<BaseType, std::string> >
MyFactory;

Now, in the client code I only use the base class interface, like:

BaseType *object = MyFactory::Instance().CreateObject("DerivedType");

This works, for example, if I'd compile the library (which contains the base
class, derived classes
and factory) to be a dynamically loaded one, since all translation units in
the library will be included.

But if I compile to a static library, the linker won't see any of the
derived classes being used (implicitly)
and will drop them from the final module.

Currently, I have implemented a hack solution - to register all derived
classes in a centralized
place (for example a static method BaseType::RegisterAllDerived()).. but as
you can see, it's not
very pretty.

I found no way to tell the linker to link with all objects in the library,
except by specifying
the objects outside the library - therefore not using a library (I am using
VC++7.1).

I understand that this isn't a serious issue, since in a library there can
be a only a finite amount
of types to register... But it seems too hacky.

Any insight about this?

PS. This linker optimization has other consequences, for example dropping
win32 resource objects
in a static library.
Jul 22 '05 #1
1 2231
adeht wrote:

I understand that this isn't a serious issue, since in a library there can
be a only a finite amount
of types to register... But it seems too hacky.

Any insight about this?


A similar problem can occur with persistent classes. The
application may have no direct knowledge of all the derived
classes using only the base class directly in the code.

We have a perl script that zips through all the headers
looking for classes that are derived from Base. It then
creates a file containing a function that 'uses' all the
classes and a call such that the linker requirements are
satisfied. I can't recall the details as this magic has
worked for the last 8 years, but the file looks something like:

// This file is auto-generated DO NOT modify

#include "derived1.h"
#include "derived2.h"
#include "derivedN.h"

bool derived_linker_loader(bool initialise)
{
if (initialise) {
static derived1* d1 = new derived1;
delete d1;
static derived1* d2 = new derived2;
delete d2;
static derivedN* dN = new derivedN;
delete dN;
}
return !initialise;
}

then in the persistence subsystem there is the call

derived_link_loader(false);

the script is run after all code updates before compilation
and linking.

HTH

Jul 22 '05 #2

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

Similar topics

0
by: TGF | last post by:
Hello, I am creating a console app. I try to link a static library by including it in the Linker-Input-Additional Dependencies field under the Project properties. Then I specify the path to...
0
by: TGF | last post by:
Hello, I am creating a console app. I try to link a static library by including it in the Linker-Input-Additional Dependencies field under the Project properties. Then I specify the path to...
4
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown...
2
by: Joske | last post by:
Hi, I'm having a similar problem as Scott. A static library with managed C++ functions fails to link in. See http://groups.google.be/groups?q=managed+C%2B%...
2
by: Robbie Hatley | last post by:
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote: > Robbie Hatley wrote: > > > > I ran into a problem a few days ago when I added a couple of > > template functions to one of my personal...
4
by: DaTurk | last post by:
Hi, I've noticed that when trying to create a managed, auto_gcrooted variable in a antive class, the compiler throws linker exceptions. While searching some forums I found out that this is a...
2
by: Oneironaut | last post by:
Hello friends, I have an issue with a linker warning. It is the warning LNK4089. I am working in MSVC6.0 I investigated and this warning tells that the import of the library to which it makes...
2
by: NvrBst | last post by:
I have C99 code (which I changed extensions to .cpp to compile as C+ +98). Which is does fine. I create a Static Library Project (.lib) and Build, and it builds the .obj files (9 of them) and...
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: 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...
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.