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

DLL With Static Library Initialization Conflict

I have an application that requires a DLL and an executable that uses the
DLL, both of which were implemented in Visual C++ using unmanged code. Both
the executable and the DLL are linked with functions that are stored in a
static library. During initialization of the executable, classes and static
globals within functions linked from the static library appear to be
initialized twice, once when the executable initializes the run-time code,
and once when the DLL initializes the run-time code. This dual
initialization is not always benign and I am at a loss of how to stop it.

Presumably, there must be some method of including functions from a static
library once, and once only, either in the DLL or in the referencing
executable.

Perhaps this situation has been encountered before. If so, any suggestions
would be appreciated.

--
================================================== ====================
================================================== ====================
==
== Bob Riedel
== Beckman Coulter, Incorporated
== PO Box 8000 W-529
== 200 S Kraemer Blvd
== Brea CA 92822-8000
==
== Email 1: ra******@beckman.com
== Email 2: ra******@mindspring.com
==
==
== The opinions expressed are my own, and do not necessarily represent
== those of Beckman Coulter, Inc.
==
================================================== ====================
================================================== ====================
==
== "Effective education is the key to successful democracy."
==
== "Criticizing the actions of others offers so little risk, and
== requires so little effort that it is, without exception, the tool
== of the lazy and of the foolish -- who have neither the intelligence
== to discover, nor the discipline to pursue, a realistic
== alternative."
==
================================================== ====================
================================================== ====================
Nov 17 '05 #1
8 2990
Robert A Riedel wrote:
I have an application that requires a DLL and an executable that uses the
DLL, both of which were implemented in Visual C++ using unmanged code. Both
the executable and the DLL are linked with functions that are stored in a
static library. During initialization of the executable, classes and static
globals within functions linked from the static library appear to be
initialized twice, once when the executable initializes the run-time code,
and once when the DLL initializes the run-time code. This dual
initialization is not always benign and I am at a loss of how to stop it.

Presumably, there must be some method of including functions from a static
library once, and once only, either in the DLL or in the referencing
executable.

Perhaps this situation has been encountered before. If so, any suggestions
would be appreciated.


I can think of two solutions. I'll start with the more general one.

As your EXE and DLL are linking statically to the library, they each get
their own, private copies of the library's code and data. For them to share
this library's code and data, you will have to turn it into a DLL, which
they both link to. A similar situation exists with the CRT. Modules which
link statically to it have independent CRT state, including heap, file
descriptors, errno, and so on, which creates module boundary problems in
which memory allocated by the DLL can't be freed in the EXE, FILE*'s can't
be passed between modules, and so on. Linking everyone to the same CRT DLL
avoids these problems and makes your program act much more like a statically
linked program.

Alternatively, you could have your DLL link to the static library and define
a forwarding interface for use by the EXE, which would no longer link to the
static library. Of course, this could be very inconvenient.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
This is a helpful response. It is the solution that I expected, even though
I had hoped to avoid it. Thank you for the reply.
"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:gv********************************@4ax.com...
Robert A Riedel wrote:
I have an application that requires a DLL and an executable that uses the
DLL, both of which were implemented in Visual C++ using unmanged code. Boththe executable and the DLL are linked with functions that are stored in a
static library. During initialization of the executable, classes and staticglobals within functions linked from the static library appear to be
initialized twice, once when the executable initializes the run-time code,and once when the DLL initializes the run-time code. This dual
initialization is not always benign and I am at a loss of how to stop it.

Presumably, there must be some method of including functions from a staticlibrary once, and once only, either in the DLL or in the referencing
executable.

Perhaps this situation has been encountered before. If so, any suggestionswould be appreciated.
I can think of two solutions. I'll start with the more general one.

As your EXE and DLL are linking statically to the library, they each get
their own, private copies of the library's code and data. For them to

share this library's code and data, you will have to turn it into a DLL, which
they both link to. A similar situation exists with the CRT. Modules which
link statically to it have independent CRT state, including heap, file
descriptors, errno, and so on, which creates module boundary problems in
which memory allocated by the DLL can't be freed in the EXE, FILE*'s can't
be passed between modules, and so on. Linking everyone to the same CRT DLL
avoids these problems and makes your program act much more like a statically linked program.

Alternatively, you could have your DLL link to the static library and define a forwarding interface for use by the EXE, which would no longer link to the static library. Of course, this could be very inconvenient.

--
Doug Harrison
Microsoft MVP - Visual C++

Nov 17 '05 #3
While attempting to implement this suggestion, I am having difficulty
interpreting a semanic issue with respect to the __declespec(
dllexport/dllimport ) keyword. Suppose for example a class is declared in a
header file:

//Header file 'a.h' :

#ifndef API
#define API __declspec( dllimport )
#endif

class
API
a
{
void do_some_stuff( void ) const ;
} ;

// Implementation file:

#define API __declspec( dllexport )

#include "a.h"

namespace {

struct B { int n ; } ;

B _b ;

} // END:: anonymous namespace

a::do_some_stuff( void ) const
{
_b.n++ ;
}

// END implementation file.

Will the static global _b declared in the anonymous namespace be properly
initialized by the CRT when the DLL startup code runs without specifying any
specific attributes?

If I understand the way a DLL should work, I would claim that static global
_b in the anonymous namespace would be instantiated in the DLL's private
segment when a process attaches.

Is this correct?

A related issue concerns the instantiation of template classes. If I
understand the way that would work, a template class cannot be given the
dllexport attribute, however, one could explicitly instantiate the class and
specify the attribute.

For example:

template <class T>
class A
{
} ;

template class __declspec( dllexport ) A<char> ; // Legal, right ???

Although I am puzzled about how exactly the import would be declared in a
header file. Perhaps:

typedef A<char> mytype ;

template class __declspec( dllimport ) A<char> ;

Or is there some other way?

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:gv********************************@4ax.com...
Robert A Riedel wrote:
I have an application that requires a DLL and an executable that uses the
DLL, both of which were implemented in Visual C++ using unmanged code. Boththe executable and the DLL are linked with functions that are stored in a
static library. During initialization of the executable, classes and staticglobals within functions linked from the static library appear to be
initialized twice, once when the executable initializes the run-time code,and once when the DLL initializes the run-time code. This dual
initialization is not always benign and I am at a loss of how to stop it.

Presumably, there must be some method of including functions from a staticlibrary once, and once only, either in the DLL or in the referencing
executable.

Perhaps this situation has been encountered before. If so, any suggestionswould be appreciated.
I can think of two solutions. I'll start with the more general one.

As your EXE and DLL are linking statically to the library, they each get
their own, private copies of the library's code and data. For them to

share this library's code and data, you will have to turn it into a DLL, which
they both link to. A similar situation exists with the CRT. Modules which
link statically to it have independent CRT state, including heap, file
descriptors, errno, and so on, which creates module boundary problems in
which memory allocated by the DLL can't be freed in the EXE, FILE*'s can't
be passed between modules, and so on. Linking everyone to the same CRT DLL
avoids these problems and makes your program act much more like a statically linked program.

Alternatively, you could have your DLL link to the static library and define a forwarding interface for use by the EXE, which would no longer link to the static library. Of course, this could be very inconvenient.

--
Doug Harrison
Microsoft MVP - Visual C++

Nov 17 '05 #4
Robert A Riedel wrote:
While attempting to implement this suggestion, I am having difficulty
interpreting a semanic issue with respect to the __declespec(
dllexport/dllimport ) keyword. Suppose for example a class is declared in a
header file:

//Header file 'a.h' :

#ifndef API
#define API __declspec( dllimport )
#endif

class
API
a
{
void do_some_stuff( void ) const ;
} ;

// Implementation file:

#define API __declspec( dllexport )
That's not the best way to use those __declspecs; see this message for a
better way:

http://groups.google.com/groups?selm...ocbo%404ax.com
#include "a.h"

namespace {

struct B { int n ; } ;

B _b ;

} // END:: anonymous namespace

a::do_some_stuff( void ) const
{
_b.n++ ;
}

// END implementation file.

Will the static global _b declared in the anonymous namespace be properly
initialized by the CRT when the DLL startup code runs without specifying any
specific attributes?
Yes.
If I understand the way a DLL should work, I would claim that static global
_b in the anonymous namespace would be instantiated in the DLL's private
segment when a process attaches.

Is this correct?
Yes. It will be in the DLL's data segment, and it will be initialized and
destroyed when the DLL receives the DLL_PROCESS_DETACH and
DLL_PROCESS_DETACH notifications. But see "DllMain" in MSDN for some
important restrictions which apply to DLL global data initialization, which
is performed from DllMain context. See this message for a problem which can
affect local static data in DLLs:

http://groups.google.com/groups?selm...bcf3%404ax.com
A related issue concerns the instantiation of template classes. If I
understand the way that would work, a template class cannot be given the
dllexport attribute, however, one could explicitly instantiate the class and
specify the attribute.

For example:

template <class T>
class A
{
} ;

template class __declspec( dllexport ) A<char> ; // Legal, right ???

Although I am puzzled about how exactly the import would be declared in a
header file. Perhaps:

typedef A<char> mytype ;

template class __declspec( dllimport ) A<char> ;

Or is there some other way?


That syntax is almost right. See these articles for more:

HOWTO: Exporting STL Components Inside & Outside of a Class
http://support.microsoft.com/?kbid=168958

Explicit Instantiation
http://msdn.microsoft.com/library/de...tantiation.asp

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #5
While porting the library to a DLL, there are numerous occassions where a
C4251 compiler warning is encountered on certain class private data. Since
clients of these classes have no reason to access the private data section,
are these warnings material and do they need to be remediated?
"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:9b********************************@4ax.com...
Robert A Riedel wrote:
While attempting to implement this suggestion, I am having difficulty
interpreting a semanic issue with respect to the __declespec(
dllexport/dllimport ) keyword. Suppose for example a class is declared in aheader file:
..
..
.. --
Doug Harrison
Microsoft MVP - Visual C++

Nov 17 '05 #6
Robert A Riedel wrote:
While porting the library to a DLL, there are numerous occassions where a
C4251 compiler warning is encountered on certain class private data. Since
clients of these classes have no reason to access the private data section,
are these warnings material and do they need to be remediated?


You should make sure that no inline function accesses the data member. This
includes default ctor, copy ctor, dtor, and assignment operator the compiler
generates for you. You should define all these members out of line. Then if
the compiler still complains about C4251, you should be able to ignore it.

See these messages for more on C4251:

http://groups.google.com/groups?selm...kf11%404ax.com
http://groups.google.com/groups?selm...mbdd%404ax.com
http://groups.google.com/groups?selm...1ob0%404ax.com

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #7
Thanks for the info.

The other thing that it seems possible to do is to only export the public
functions and data, instead of the entire class.

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:de********************************@4ax.com...
Robert A Riedel wrote:
While porting the library to a DLL, there are numerous occassions where a
C4251 compiler warning is encountered on certain class private data. Sinceclients of these classes have no reason to access the private data section,are these warnings material and do they need to be remediated?
You should make sure that no inline function accesses the data member.

This includes default ctor, copy ctor, dtor, and assignment operator the compiler generates for you. You should define all these members out of line. Then if the compiler still complains about C4251, you should be able to ignore it.

See these messages for more on C4251:

http://groups.google.com/groups?selm...kf11%404ax.com http://groups.google.com/groups?selm...mbdd%404ax.com http://groups.google.com/groups?selm...1ob0%404ax.com
--
Doug Harrison
Microsoft MVP - Visual C++

Nov 17 '05 #8
Robert A Riedel wrote:
Thanks for the info.

The other thing that it seems possible to do is to only export the public
functions and data, instead of the entire class.


That can often work, but exporting/importing the whole class does something
special. See this message for more on that:

http://groups.google.com/groups?selm...9d%40philiplu2

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #9

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

Similar topics

1
by: mccoyn | last post by:
I'm porting an old project to use .NET. When I try to link in a static library (.lib) that has a single managed class in it. I get the following errors: LINK : error LNK2020: unresolved token...
2
by: free2cric | last post by:
Hi, I have two c++ static libraries say A.lib and B.lib. I am creating third static library C I create project in microsoft visual studio as win32 static library. I go to project-> settings ->...
3
by: mpatnam | last post by:
I have an executable which links to a static library (.a). I want to provide a hook by overriding a function part of this static library. Eg: I have a function "int blkstart(int i)" in this static...
4
by: newest newbie | last post by:
I'm new to using c#, but what I'm trying to do is create a library control containing a form which in turn contains a Windows application that I have defined in another library. I'm able to do...
3
by: Manny Silva | last post by:
Hi, I would like to create a static library that uses and in effect adds to another static library. I could simply add functionality to the existing library, but functionally it doesn't really...
15
by: Notre Poubelle | last post by:
Hello, I have a large legacy MFC application. As is typical, there is an executable along with several MFC DLLs. One of these DLLs is created by staticly linking in many libraries resulting in...
4
by: mnowosad | last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time an executing code within an AppDomain references a class for the the first time since the AppDomain was created/loaded,...
4
by: sealo | last post by:
Hello, I have a test in VS2005 that static library A.lib use the dynamic library C.dll. Then a application App use the A.lib. App-->A.lib-->C.dll It works. But if I remove the C.dll, the app...
6
by: Even | last post by:
Hi all, As far as I know, relative address will be assigned by program at link time.Right? Ok, here we go. So if we use a static library, it will give the program an relative address(i mean...
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: 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
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
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...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.