Solved

Creation of global class in a DLL

Posted on 2011-03-11
6
391 Views
Last Modified: 2012-08-13
Hi,

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.

Objectives:
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?

Thanks,
- Vashren
#include "TestObject.h"

TestObject g_testObj;

//----------------------------------------------------
// DllMain()
//----------------------------------------------------
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, VOID* pReserved)
{
    g_testObj.PublicFunc();

    switch (dwReason)
    {
        case DLL_PROCESS_ATTACH:
            // some init of testObj
            break;
        case DLL_PROCESS_DETACH:
            // some cleanup of testObj
            break;
        default:
            break;
    }

    return TRUE;
}

Open in new window

0
Comment
Question by:Vashren
  • 3
  • 3
6 Comments
 
LVL 86

Expert Comment

by:jkr
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'?
0
 

Author Comment

by:Vashren
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?
0
 
LVL 86

Accepted Solution

by:
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Vashren
ID: 35110455
Awesome - it builds correctly now.

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

Assisted Solution

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

BTW, you might be interested in the http://en.wikipedia.org/wiki/Singleton_pattern - this takes the whole concept of 'global objects' to a new pattern. See also http://www.codeproject.com/KB/cpp/singleton_template.aspx ("A Singleton Template Class") for an example.
0
 

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
PHP question(s) about order of output 9 57
Export Table to CSV - Access to CSV - using python 18 96
Apps blocked by Java 9 55
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

929 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now