Solve the problem of repeated definition of global variables in the C + + language

Source: Internet
Author: User

Objective

Today, when sorting out your code, consider that the code I wrote from one to the end is in a CPP file. So, you want to separate the modules in your code so that you can read and manage them better.

Problems encountered

My approach is to:

    1. Macro definitions, struct definitions, function declarations, and global variable definitions are placed in a head.h header file
    2. The definition of the function is placed in the Head.cpp
    3. The main function is placed in the main.cpp

However, the error indicates that the XXX variable is defined in the *.obj file.

Why the problem occurred

Why does this happen?

    1. First, the compilation of individual files is independent. Compile to Main.obj in Head.cpp compilation to Head.obj,main.cpp. This process has no error, which means that the compilation process is not a problem.
    2. Next is the link to obj. When linking Main.obj and Head.obj, the compiler discovers that Head.obj allocates memory space for these global variables, and that the global variables are allocated memory space in Main.obj.
    3. There are two different memory addresses for the same variable. The compiler then gave an error.
It's not a way.

Add static to the global variables of the header file inside the head.h. Compilation can be passed, but there are other problems inadvertently.

Static only extends the lifetime of the variable and also restricts the variable to the current file. The reason why it can be used in main.cpp is because it replicates a variable with the same variable name at compile time to main.cpp. Then the change of the "global variable" inside the main.cpp does not change the value of the global variable inside the original head.h.

Although the compilation passed, but the program is wrong.

The real Solution
    1. Place the global variable definition in the Head.cpp file.
    2. The declaration of the global variable is stored in Head.h, and is modified before each declaration extern .
My personal thoughts.

I think one of the things you can do to be able to separate global variables is:

    1. The global variable definition is still placed in the head.cpp.
    2. Create a new global.h header file that holds the declaration of the global variable and modifies it before each declaration extern .
    3. The global.h header file is included when other files need to use global variables.
Knot words

The problem arises because the C language has not been used for too long. Moreover, in the use of C or C + + language, often because the experiment and the class set up need to write the code is not too much, so developed a habit, a main.cpp write to the end. When it comes to separating its own module code, it is not possible to find out that a defined global variable causes a compile link error. So write down this article beware of yourself! The article may have the wrong place, hoped that everybody can correct!

Article from Kwongtai ' blog, reproduced please indicate the source

Solve the problem of repeated definition of global variables in the C + + language

Related Article

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.