473,839 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linking .lib files with static variables

Hi All,

I'm having some trouble with a .lib file in a project I'm working on.

The file contains a class which implements the singleton pattern.

To implement it I've declared a static pointer to the class called instance
outside of the class. The code looks something like this(the project is very
large and I haven't been able to build a cut-down version of the problem
because I'm not familiar with all the settings):

static Singleton* instance = 0;

class Singleton
{
public:
static Singleton* getInstance()
{
if(instance == 0)
{
instance = new Singleton();
}
return instance;
}

.....
}

So other classes that wish to use the class call getInstance() which
instantiates a new instance or returns the currently existing one.

Now the problem seems to arise because two DLL's link to the library
containing the class. So when one dll calls the getInstance method he get's
one instance (always the same for inside that dll) but when the second one
calls it he gets a new instance at an entirely new address....

I've tried changing the way my singleton is implemented but have not been
able to implement it using any other method so far. I don't even really think
that that is the problem. I'm starting to think that no matter how I
implement the class it's going to be returning two different instances of the
class.

Has anyone encountered anything like this before? I have a feeling that it
is some sort of linker or VC++ specific problem that I'm having.

Cheers
Johnny
Nov 17 '05 #1
3 1370
>....

So other classes that wish to use the class call getInstance() which
instantiates a new instance or returns the currently existing one.

Now the problem seems to arise because two DLL's link to the library
containing the class. So when one dll calls the getInstance method he get's
one instance (always the same for inside that dll) but when the second one
calls it he gets a new instance at an entirely new address....


Johnny,

You need to arrange to only have a single instance. As soon as you
have the same data in 2 DLLs you're going to get in the situation you
are in.

I'd change things so that I only had that code and data in a single
DLL and I'd export a getInstance() function from it for other
modules. If you want to export the class, have a look at "Using
dllimport and dllexport in C++ Classes" in MSDN.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #2
Thanks for the reply.

So now for implementing your solution... :)

I'm actually working with a mostly COM based application (well I think,
COM/MFC etc... etc... are all MS crap that I have never really learnt and
prefer to use it as little as possible) written for VS6 which has been
re-compiled with VS .NET 2003 and most of the projects are compiled with
/clr.

My singleton class is an unmanaged class which contains a managed object
referenced by gcroot<>. This class was a replacement for another class in the
lib file referenced by the two Dll's. As you know this screwed my singleton
implementation. So now I've decided to remove the project which built the lib
file (It's no longer necessary) and replace it with a .NET Class Library
containing my singleton class.

So I've got my class library building correctly but I can't seem to use it
in the two Dll's which require the class.

I've included the #using "Singleton. dll" in the necessary places but the
compiler does not seem to be able to recognise my Singleton class (I get lots
of C2065's).

There appears to be a few possibilities here:
1. I'm not including the Singleton Dll in the unmanaged code in the correct
way.
2. I'm not building the right type of Dll. I need to go for the old pre-.NET
Dll. (Which I don't know how to build, so if you can get me started that'd be
nice)
3. Do I need to link the Dll the old-fashioned way using the header file and
lib file? (My .NET class library doesn't generate a lib file so if this is a
solution you'll have to tell me how to do it).
4. I've got no idea how to do interop and need a good beating.

Both the Dll's are compiled with /clr and previously had no problem using my
singleton class (except that it wasn't really a singleton, which screws
things up greatly).

So any idea's on what I'm doing wrong?

Thanks for your help.
Johnny

"David Lowndes" wrote:
....

So other classes that wish to use the class call getInstance() which
instantiates a new instance or returns the currently existing one.

Now the problem seems to arise because two DLL's link to the library
containing the class. So when one dll calls the getInstance method he get's
one instance (always the same for inside that dll) but when the second one
calls it he gets a new instance at an entirely new address....


Johnny,

You need to arrange to only have a single instance. As soon as you
have the same data in 2 DLLs you're going to get in the situation you
are in.

I'd change things so that I only had that code and data in a single
DLL and I'd export a getInstance() function from it for other
modules. If you want to export the class, have a look at "Using
dllimport and dllexport in C++ Classes" in MSDN.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Nov 17 '05 #3
I'm afraid that I'm no help to you when it comes to managed code -
I've shied away from it and stuck with the devil I (mostly) know until
such time as I really need to make a move.

The only thing that comes to mind is that the complexity of your
existing projects may be confusing the issue, I'd be inclined to
create new projects just to prototype the arrangements between them
and be sure it'd work - it might be easier to work with that way?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #4

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

Similar topics

0
2365
by: Dibyendu Roy | last post by:
Hi All, I build an object called "dblorcle" to connect to oracle database in Sun solaris box. This is built linking with various oracle ".a" (archived, for static linking) files come with standard oracle library. I use the following command while linking: ld -L(oracle lib path) -lnetv2 -lnttcp -lnetwork -lncr -lnetv2 -lnttcp -lnetwork -lclient -lcommon -lgeneric -lmm -lnlsrtl3 -lcore4 For each element starting with -l has...
3
5124
by: Charles Nicholson | last post by:
Hello all- I have some static C++ libraries that I wrote in VS2003 but which upgraded fine when i went to VS2005 Pro. In them i overload the global versions of operators new, new, delete, and delete. I also use the STL and parts of boost (shared_ptr<> and weak_ptr<>) pretty heavily. They have dependencies on various windows libs (dbghelp, winsock, etc...). These static libraries have no common runtime support at all and use the...
6
2582
by: Chris Stankevitz | last post by:
At link time, MSVC determines some of my libraries are unused and doesn't link them into my exe. This undesirable feature causes problems when I employ the factory pattern. With the factory pattern, the app decides at run time which code to use. Is there a link option to turn of this feature? MSVC 7.1 .net 2003 69462-270-0000007-18536 Thanks,
0
2560
by: Philip Lowman | last post by:
I am in the process of trying to migrate a couple of build solutions to Visual Studio Express 2005 from VS 2003 Professional and I am running into a weird C/C++ runtime library linking issue when using the /MT compilation option. Our debug solution's /MTd flag works fine and using /MD also seems to work ok. For some reason I can't fathom, when I use /MT, linking the static excutable completely dies (problems resolving symbols in the STL,...
0
1908
by: capes | last post by:
have functions in my cell model calling CVODE for each time step. When I build the application I get a number of link errors: \cardiacsimulator.o Release\ccardiaccell.o Release\ccelldata.o Release\cgapjunction.o Release\cheart2d.o Release\integrator.o -o D:\kp\programs\cardiacsimulator\Release\cardiacsimulator.exe Release\integrator.o: In function `func_f': /cygdrive/d/kp/programs/cardiacsimulator/integrator.cpp:70: multiple...
14
13439
by: John Goche | last post by:
Hello, The extern keyword can be used in C and C++ to share global variables between files by declaring the variable in header file and defining it in only one of the two files. For example, if x were not declared extern in the header file below then the linker would complain about x being multiply defined.
2
1719
by: Mohsen A. Momeni | last post by:
Hi, Does it have any difference in security, whether to compile a function as a static lib and link it with a program or just add the function to the source? In other words, suppose we have two files, func.c containing a function which is called in main and main.c containing the main function. what is the difference when we link func.o with main.o to make a binary, with linking func.lib with main.o to make the binary, concerning security...
1
9926
by: Jaco Naude | last post by:
Hi, I'm using a static library in my application which links fine except for a few global variables. The static library only contains a bunch of .cpp and .h files and the global variables are defined as follows: extern unsigned mgl_numg, mgl_cur; extern float mgl_fact; extern long mgl_gen_fnt; extern short mgl_buf_fnt;
4
5828
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public Element_common<L2, Side<2,2 { public:
0
9697
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
10586
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10647
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
10293
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7017
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
5682
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...
1
4484
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.