Static Library and DLL Interaction

Started by
5 comments, last by craymail 21 years, 11 months ago
Hey all. I will do my best to describe my problem, but don''t hesistate to question any part of this post. I recently started a project that, like most games, want API independence. I have created a Core class (a static library) that is the starting point of the entire game. I have defined an abstract base class, RenderSystem, that defines the DLL attempted to be loaded. I load the DLL successfully and assign pRenderSystem, a pointer, in the Core class to the class created within the RenderSystem DLL (either OpenGL or Direct3D). This part all works fine, but here is what doesn''t. I have to access the Core from within each individual RenderSystem DLL for Platform specific functions, so, through the use of a singleton on the Core and making the Core library a dependency of the DLL, I try to do so. For example, I use Core::getSingleton()->FunctionHere. The problem is that from within the DLL when I call Core::getSingleton it creates a new Core as pSingleton is set to NULL, whereas, in the exe it set to the address of the loaded Core. Any suggestions as to why the DLL is not able to access the correct value of the static pSingleton pointer? Cray
CrayLead Programmer, MTA (http://www.mtavc.com)cray@multitheftauto.com
Advertisement
I think your problem is in dll exporting/importing. I found a really nice templated singleton class that broke when I wanted to dll-export the objects. How do you implement your singleton pattern? Who stores the object itself? Are you using templates? If you could post the singleton-related code, it will help.
---visit #directxdev on afternet <- not just for directx, despite the name
If I understand this correctly you have the singleton in a static library which you link into both your exe and the renderer dll. This will cause the exe and dll to each have a version of the singleton.

You should either make the static library into a dll (making the exe and renderer dll share that same singleton), or you could have a function in the renderer dll that you call, passing the singleton as a parameter (i.e. the exe 'gives' it's singleton to the dll, the renderer dll never creates it's own).

[edited by - Dactylos on April 29, 2002 2:24:54 PM]
I had a template for the singleton at one point, but when these problems started occuring I went back to what I knew worked for sure.

An example of my Core class and Singleton setup

---Core.h---
class Core
{
public:
static Core * getSingleton (void);
private:
static Core * pSingleton;
}
---Core.cpp---
Core * Core:Singleton = 0;
Core * Core::getSingleton (void)
{
if (!pSingleton)
{
pSingleton = new Core ();
}
return pSingleton;
}

I honestly do not think it is my Singleton that is causing the problem.

Cray
CrayLead Programmer, MTA (http://www.mtavc.com)cray@multitheftauto.com
Dactylos, good point.

I guess whenever loading the dll, lets say loadDLL, I could do something like loadDLL (UECore::getSingleton ()), which would pass the address of the Core object to the DLL.

I will try that if we cannot figure anything else out. Thanks for that though!

Cray

[edited by - craymail on April 29, 2002 2:34:01 PM]
CrayLead Programmer, MTA (http://www.mtavc.com)cray@multitheftauto.com
Try dll-interfacing getSingleton. In the module where the singleton is stored, declare it as __declspec(dllexport) and define it. In all other modules, declare it __declspec(dllimport) and don''t define it.

Are you sure you have only one copy of Core object for all modules? [methinks that having getSingleton definition in dll without also having the pSingleton defined in that dll won''t link].
---visit #directxdev on afternet <- not just for directx, despite the name
I implemented Dactylos solution last night and it works perfectly.

I may try the method that you are proposing IndirectX in the future, but for now this will get me over all immediate hurdles and let me continue development.

I want to thank you both for all of your help.

Cray
CrayLead Programmer, MTA (http://www.mtavc.com)cray@multitheftauto.com

This topic is closed to new replies.

Advertisement