Definition and declaration of C + + global variables

Source: Internet
Author: User

Compilation unit

The compilation is divided into two steps:

Step one: Compile each. cpp or. c and corresponding. h files in obj file (contains precompiled, compiled, compiled)

Part II: Link the obj file to generate the final executable file

There are generally two types of errors that can be categorized according to this phase:

One, compile-time errors, mostly syntax errors

One, link-time error, mainly variable, function definition error

The compilation unit refers to each obj file that is generated during the compilation phase

An obj file is a compilation unit

A. cpp or. c and its corresponding. h file together make up a compilation unit

One consists of a number of compilation units, each of which contains the relative address of the variable store, etc.

Declarations and definitions

When a function or variable is declared, it does not allocate actual physical memory space, and he can sometimes ensure that the program is compiled by

When a function or variable is defined, it has the actual physical space in memory

If the external variable referenced in the compilation unit is not defined anywhere in the project, even if it is passed at compile time, the link will be error-free because the variable is not found in the program memory

A function can be declared more than once, but can only be defined once

extern effect

Function 1, when used in conjunction with "C", such as extern "C" void fun (int a, int b), then the compiler fun this function name according to the C language rules, instead of the C + + provisions named. Therefore, C-functions can be called in C + + using this Law

Function 2, when he does not with "C" in the modification of variables or functions, such as in the header file, extern int g_num; its function is to declare the function or variable scope of the keyword, its declared functions and variables can be used in this compilation unit or other compilation unit

That is, when the B compilation unit refers to the global variables or functions of a compiled unit, the B compilation unit contains only the header file of the a compilation unit, and in the compilation phase, the B compilation unit cannot find the function or variable, but does not error, and he will find the definition from the target file of the a compilation unit at link time.

Global Variables (extern)

There are two classes that need to use a common variable, which is defined as a global variable. Typically declared in a header file, defined in CPP, defined only once

Static global variable (statics)

Variables that use the static modifier cannot be decorated with extern.

The Declaration and definition of a static modified global variable is done at the same time, when you declare a global variable with static in the header file, and do not define it.

The scope of a static modified global variable is only within its own compilation unit, and the global static variable with the same name for different compilation units is not associated, and the physical space occupied is completely independent.

Global static variables are generally defined in the. cpp

Global Constants (const)

When const is used alone, its characteristics are the same as static (the address in each compilation unit is different, but because it is a constant, it cannot be modified, so it does not matter much)

const, when used with extern, has the same characteristics as extern but cannot be modified

Definition and declaration of C + + global variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.