Use of extern in C++ dll
Posted
by
dom_beau
on Stack Overflow
See other posts from Stack Overflow
or by dom_beau
Published on 2013-11-01T15:50:13Z
Indexed on
2013/11/01
15:53 UTC
Read the original article
Hit count: 204
I declare then instantiate a static variable in a DLL.
// DLL.h
class A
{
//...
};
static A* a;
// DLL.cpp
A* a = new A;
So far, so good... I was suggested to use extern
rather than static
.
extern A* a; // in DLL.h
No problem with that but the extern
variable must be declared somewhere. I got Invalid storage class member.
In other words, what I was used to do is to declare a variable in a source file like this:
// In src.cpp
A a;
then extern
declare it in another source file in the same project:
// In src2.cpp
extern A a;
so it is the same object a at link time. Maybe it is not the right thing to do?
So, where to declare the variable that is now extern?
Note that I used static declaration in order to see the variable instantiated as soon as the dll is loaded.
Note that the current use of static
works most of the time but I think I observe a delay or something like this in the variable instantiation while it should always be instantiated at load time. I'm investigating this problem for a week now and I can't find no solution.
© Stack Overflow or respective owner