Analyze the usage and traps of static, extern, and multiple definition in C by examples

Source: Internet
Author: User

In JNI, we often encounter this scenario: to write some global variables in a. h file, and then all cpp files can be used. There is a. h file as follows:

/* * a.h * *  Created on: 2014-4-16 *      Author: Administrator */#ifndef A_H_#define A_H_int mAge = 0;void setAge(int age);int getAge();#endif /* A_H_ */

There is a variable mAge and two interfaces for setting and reading.

The following is the. cpp file:

/* * a.cpp * *  Created on: 2014-4-16 *      Author: Administrator */#include "a.h"void setAge(int age){mAge = age;}int getAge(){return mAge;}
Then there is a main. cpp file:

// ================================================ ==================================================================== // Name: test2.cpp // Author: yan // Version: // Copyright: Your copyright notice // Description: Hello World in C ++, ansi-style // ========================================== ========================================================== = # include
 
  
# Include "a. h" using namespace std; int main () {cout <"!!! Hello World !!! "<Endl; // prints !!! Hello World !!! SetAge (22); cout <"age =" <
  
   
An error is reported after compilation:
   

E: \ WorkSpaces \ Eclipse_MinGW_C \ Test2 \ Debug/../src/Test2.cpp: 12: multiple definition of 'main'
Src \ main. o: E: \ WorkSpaces \ Eclipse_MinGW_C \ Test2 \ Debug/../src/main. cpp: 13: first defined here
Collect2.exe: error: ld returned 1 exit status
Build error occurred, build is stopped

The multiple definition of error is reported. The solution is to make the variable static. There is no error in static, but there is a big trap hidden here:

Main. cpp is as follows:

# Include
    
     
# Include "a. h" using namespace std; int main () {cout <"!!! Hello World !!! "<Endl; // prints !!! Hello World !!! SetAge (22); cout <"age =" <
     
      
The output is as follows:

!!! Hello World !!!
Age = 0
Age = 555

That is, even if the set is set to 22, the value of mAge is still initialized. If the get interface is used, the value is correct. The reason is: according to the book, static variables are stored in static storage areas like general global variables. But static limits the variable access permission and only allows access to this file. Why is there direct access in main in static? The reason is that static variables are declared in the H file rather than in the cpp file. During compilation, include is similar to directly overwriting. Therefore, the initialization value obtained by directly accessing mAge is not only useless in set. This is a trap !!!

In fact, this is not safe. More variables are declared in the cpp file. Therefore, the safer method is:

1. variables are declared in cpp and declared as common types. Then set and access through the set and get methods.

2. Declare the variable in cpp and declare the same variable in the H file as an extern int mAge. In this way, all hfiles that contain the variable can directly access the mAge.

3. static and extern are grams. Once the variables in the cpp file are declared static, extern cannot be used in the H file.

4. You can also declare common variables in the H file and extern in cpp, but it is not recommended. The variable is still good in cpp.

At least include "***. cpp" should not be used.

Refer:

Http://bbs.csdn.net/topics/390626289? Page = 1

Http://blog.csdn.net/luo6620378xu/article/details/8511312





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.