Creation of global class in a DLL

Posted on 2011-03-11
Last Modified: 2012-08-13

I am building is a series of .lib files that come together to form a single DLL. I also have a class called TestObject that manages some data.

1. A single TestObject instance must be globally accessible by all functions in the DLL libs.
2. DllMain should do some init + cleanup of the global TestObject.

The immediate problem is that I can't get past the linker:

error LNK2019: unresolved external symbol "public: __thiscall TestObject::~TestObject(void)" (??1TestObject@@QAE@XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'testObj''(void)" (??__FtestObj@@YAXXZ)      mylib.lib
error LNK2019: unresolved external symbol "public: __thiscall TestObject::TestObject(void)" (??0TestObject@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'testObj''(void)" (??__EtestObj@@YAXXZ)      mylib.lib
error LNK2019: unresolved external symbol "public: void __thiscall TestObject::PublicFunc(void)" (?PublicFunc@TestObject@@QAEXXZ) referenced in function _DllMain@12      mylib.lib

See code below. Should the TestObject reside elsewhere to be global? How can I get past the linker and have TestObject be fully global?

- Vashren
#include "TestObject.h"

TestObject g_testObj;

// DllMain()
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, VOID* pReserved)

    switch (dwReason)
        case DLL_PROCESS_ATTACH:
            // some init of testObj
        case DLL_PROCESS_DETACH:
            // some cleanup of testObj

    return TRUE;

Open in new window

Question by:Vashren
  • 3
  • 3
LVL 86

Expert Comment

ID: 35110214
Well, it just seems that you have not provided both a constructor and a destructor for your class - are you linking with 'TestObject.cpp'?

Author Comment

ID: 35110315
TestObject.cpp contains an implementation of the constructor & destructor.

However, I'm not well versed with the linker. Do you mean to say I need to do something with the CPP file?
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 35110358
You need to add that file to your project, otherwise it won't be compiled or linked wirth and cause the errors you mentioned in your question. Right-click pn your projext, choose 'Add|Existing Item' and add 'TestObject.cpp'. "'Project|Add existing Item' (CTRL+SHIFT+A) will do the same.
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.


Author Comment

ID: 35110455
Awesome - it builds correctly now.

With the linker out of the way, I'll now see of the "globalness" works.
LVL 86

Assisted Solution

jkr earned 500 total points
ID: 35110923
Should work fine ;o)

BTW, you might be interested in the - this takes the whole concept of 'global objects' to a new pattern. See also ("A Singleton Template Class") for an example.

Author Comment

ID: 35112284
Thanks. Time to gobble up some points!

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Formula for calculating ROI on training 6 51
Gaming Software 1 19
C qsort compare function issue 6 18
WordPress: Debugging from my Windows 10 Desktop 6 39
This article will show, step by step, how to integrate R code into a R Sweave document
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question