VC++ defines global variables and extern usage

Basic explanation: extern can be placed before a variable or function to indicate that the definition of the variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable and function. In addition, extern can also be used for link specification.

That is to say, extern has two functions. The first one, when it is used together with "C", such as: extern "C" void fun(int a, int b); tells the compiler to compile the function name of fun Translate the corresponding function name according to the rules of C instead of C++. The rules of C++ will change the name of fun when translating the function name. It may be fun@aBc_int_int#%$ or something else. Look at the "temper" of the compiler (different compilers use different methods), why do you do this, because C++ supports function overloading, so I won't discuss this issue too much here, if you are interested, you can Go to the Internet to search, I believe you can get a satisfactory explanation!
Second, when extern does not modify variables or functions together with "C", such as in the header file: extern int g_Int; its role is to declare functions or global variables The scope of the keyword, the declared functions and variables can be used in this module or other modules, remember that it is a declaration not a definition! That is to say, if the B module (compilation unit) is defined in the reference module (compilation unit) A When the global variable or function is included, it only needs to include the header file of module A. During the compilation phase, although module B cannot find the function or variable, it will not report an error. It will generate the target from module A when linking Find this function in the code.

vc defines global variables and extern usage:

Global variables are generally defined like this:

1 . Define int myInt in a class's .cpp ;

Then extern int myInt in .cpp where you want to use it; this will work.

2 . Add to stdafx.cpp:

int myInt;

Then add to stdafx.h:

extern int myInt

After this definition, it will be visible no matter what file it is in.

Original link: vc defines global variables and extern usage

 

Global variables are generally defined like this:
1. Define int myInt in a class of .cpp; then extern int myInt in .cpp where it is to be used. 
2. Add: int myInt to stdafx.cpp; then add: extern int myInt
3 to stdafx.h. First define a Glbs.h and put all the original definitions of global variables into it. Then define an Externs.h, add extern to the variables previously defined in Glbs.h. Note: If you set the initial value in Glbs.h, do not add the value in Externs.h. Then when calling, #include <Glbs.h> for the first call,
  #include <Externs.h> for subsequent calls
4. Define variables in CApp: CString g_sUser;//Define the global
 in the class to be referenced like this: extern CSvApp theApp;//Add this sentence before the constructor of the .CPP file.
When referencing: if( theApp.m_sUserName == "") ;//Reference like this.
5. If you want to use a custom global custom class The general method is as follows, which can be used in all files.
  1. Add the header file of the class to stdafx.h 2. Add
  the definition of the class to stdafx.cpp, such as: CMyClass g_class;
  3. In the app class of your program, add extern at the beginning of the header file of the CTestProjectApp class CMyClass g_clas.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325091244&siteId=291194637