Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to create and use global variable in a dll created in c++??
Posted

Please don't. It violates good development practices. If you need to store application wide settings then you should create a static repository collection class.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Aug-13 1:52am    
Agree, a 5.
However, to be fair, a singleton pattern should be mentioned. So, crediting your answer, I added to it. Please see my answer.
—SA
I agree with Solution 1. The concept of global is really wrong. In almost all cases, it's possible to write code without any globals. However, to be fair, I would admit that in some rare and special cases it would be practical to use singleton pattern:
http://en.wikipedia.org/wiki/Singleton_pattern[^].

You should understand that even this pattern, being way more cultured than "global", is still controversial and should be used with care. Please see:
http://www-106.ibm.com/developerworks/library/co-single.html[^],
http://www-128.ibm.com/developerworks/java/library/j-dcl.html?loc=j[^],
http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial[^].

If you still need it, you can do it by yourself or find some code sample on the Web. For example:
http://msdn.microsoft.com/en-us/library/ee817670.aspx[^],
http://www.yolinux.com/TUTORIALS/C++Singleton.html[^] (just ignore the fact that this is Linux, this factor is totally irrelevant).

You can find some more: http://bit.ly/1eWaHIp[^].

—SA
 
Share this answer
 
Comments
pasztorpisti 19-Aug-13 5:56am    
+5. Static variables can become a very big problem in a large project. The usual problem is that their lifetime often spans the whole program execution so they are accessible all the time and people tend to put here things whose lifetime logically shouldn't be the same as that of the whole program. I've seen and fixed a few bugs caused by statics, the most common problem is that some codepieces try to access these globals for example during shutdown when objects break down. I caught the related bugs by controlling the creation/destruction of these statics and by exploiting the fact that a global static pointer initialized to zero is initialized immediately when the program code is loaded:
class Something
{
public:
static void Create();
static void Destroy();
Something& Instance()
{
// g_Intance is a pointer
assert(g_Instance);
return *g_Instance;
}
};
Of course I posted this here just for the case someone had to water the fire, this is not a pattern to follow! :-)
Sergey Alexandrovich Kryukov 19-Aug-13 9:57am    
Thank you. Good points.
—SA
You create global variables in a DLL like you do in standard applications, there is nothing special with it.
If you need to export such variables you may use (on Windows platform) the dllexport attribute (see dllexport, dllimport at MSDN[^] (or better, you may export accessor functions).
 
Share this answer
 
Comments
H.Brydon 19-Aug-13 12:50pm    
Right, and if it needs to be accessed in multiple .cpp files then declare it in a common .h file and use 'extern' in the .h file...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900