declaration global variable in c++ for multiple forms project (not static variable)
-
Sunday, May 15, 2011 11:50 AMPlease show me how to define some global variables in C++/clr project, As the variables can be read and Its value can change in all Forms class in a project. Take an example for me. any one can help me???
All Replies
-
Sunday, May 15, 2011 12:22 PM
You cannot have managed types as global variables.
You can however declare unmanaged types as global variables.
Just declare a global variable just like it is done in normal C++ programs.
Declare the variable outside the scope of the namespace, for eg. int i = 100;
«_Superman_»
Microsoft MVP (Visual C++)
Polymorphism in C -
Thursday, May 19, 2011 7:18 PMI want to declare a variable such as a int or float or maybe an struct variable and change it's value in 3 form of my project. can any one take an example to show me what I must do? please help me. I'm getting mad now. I tried all the ways I understand.
-
Thursday, May 19, 2011 9:15 PM
It is pretty easy. Here is one way:
Now having said that you may well be better off from an architecture standpoint having a class with some private static members to hold values and some public static functions to access/manipulate those values.// In globals.h namespace Globals { extern int x; } // in globals.cpp int Globals::x = 8;
- Marked As Answer by Rob PanModerator Tuesday, May 24, 2011 9:25 AM
-
Monday, May 07, 2012 10:57 PM
I run into a similar problem.
I created a multi-doc MDI app.
Declared a global variable, int glbInt, in myHeader.h.
Added this header to stdafx.h.
Then initialize this glbInt in the App class and use it in mainframe and view classes.
Compile, and get error LNK2005. int GlbInt already defined in MainFrm when linking stdafx.obj.
I tried to add extern in front of int glbInt in myHeader.h, but then I would get LNK2001, Unresolved external symbol, int glbInt.
I searched MSDN, but did not find any good answer. Can someone tell me what should I do?
Thank you.
-
Tuesday, May 08, 2012 12:03 AM
You put DECLARATIONS in a header file, so all the C++ files know about a variable:
extern int var;
This means "somewhere in my program there is an int var. The linker will find it".
You put ONE DEFINITION in a C++ file.
int var;
This means "int var lives here. The linker will handle references to this variable in other C++ files".
-
Tuesday, May 08, 2012 4:40 PM
Thank you for the reply. That's seems to be exact reason it would not compile (link to be exact).
I am trying to convert a large existing project from VC6 to VC2005 (and beyond). There are a large number of global variables and constants declared in header files (declared only once between these headers). The project compiles fine in VC6 and runs properly. But it would not compile for the reason you explained.
I am trying to use #define switches so that I can reuse the header files and make it compile, but so far not successful.
I made a simple MDI project with one global var and constant in a common header file.
// commDec.h - Common declaration:
#ifndef comm_h
#define comm_h
#if defined(useExtern)
#define commA extern
#define commB
#pragma message("Note: useExtern. Define extern.")
#else
#define commA
#define commB /##/
#pragma message("Note: useExtern. Define blank.")
#endif
commA int glbInt; // = 10;
commB const int conGlbVal = 35;
#endif
Then, in stdafx.h:
#include "commDec.h"
In stdafx.cpp:
#define useExtern
#include "commDec.h"
#undef useExtern
All cpp files in the project has #include "stdafx.h" as the top.
In the project property, I set Use precompiled header for the project. It actually set all cpp files to use precompiled header, except for the stdafx.cpp, for which it is set to Create precompiled header.
This project would not compiled. Error LNK2001: Unresolved external symbol, int glbInt. Apparently, the header commDec.h was not included a second time. I tried to remove blocker comm_h, but it did not help.
How can I reuse the declarations in the header file, but use the declarations according to the compiler switches?
Thank you.
-
Tuesday, May 08, 2012 4:57 PM
I would add that there are references to glbInt and conglbVal in app.cpp and mainframe.cpp files.
-
Tuesday, May 08, 2012 6:46 PM
I found a solution, but it is not pretty.
I set the project to Use precompiled header. Then set myApp.cpp to Not use precompiled header. It compiles fine now.
-
Wednesday, May 09, 2012 12:34 AMHere is the way we generally do this kind of declaration/definition:in file.h:extern int var;in file1.cpp:#include "file.h"int var;in file2.cpp:#include "file.h"in file3.cpp:#include "file.h"You understand now that you can use "extern int var" multiple times inyour project, but you must use "int var" only once in your wholeproject.This behavior has not changed since VC++6.
-
Friday, May 11, 2012 11:20 PM
Thank you.
But when there are a long list of these global variables and constants, then it would be hard to do them in different files. That's why the preprocessor parameters are used in our case, and all variables constants are listed twice in the header files with different modifiers controlled by preprocessor switches.
In VC6, it was compiled with "Automatically use of precompiled headers" option, and this process compiles fine. But in later VC (e.g., 2005), the Auto option was removed, and there are only Create/Use/Not use options.
Thus, I have to use the work around mentioned above. It is not pretty, and may break again in the future.

