Difference between definition and declaration in C ++, involving the extern keyword

Source: Internet
Author: User
The differences and connections between the two have always been very vague, especially the extern keyword. I read c ++ primer this time and read it in the second chapter. So I had a good understanding and made some code tests. The conclusion is as follows:

1. Definition can only be used for a variable, that is, to define a variable. At this time, the memory space of the variable will be allocated. All definitions such as int I and INT I = 10 are defined because I variables are allocated with memory.

2. declaration can be used for variables or types (such as declaring a struct but not defining variables). If used for variables, the variable will not be allocated memory, in addition, you must add extern (the definition of this variable is not here, and it is extern in other places). If it is a type, there is nothing to say, the original type does not need to allocate memory. Only variables need memory. If extern is added before the declaration of the type, it is also possible, but this does not make any sense. Note: If you write: extern int I = 10;, this is also definition. Because I is assigned a value, the extern is not added.

3. No matter how many source files there are in the program and the code volume, a variable can only be definition once, but the Declaration is unlimited.

Note: here, the definition and declaration of function methods are similar. In general, if the code in the method is not written, this is Declaration. If the code is written, it is the definition of the method. Therefore, write a declaration method in the header file, such as void print_something ();, and then write the implementation of the method in the source file. In this case, void print_something (); and extern void print_something (); have the same effect, and both extern and extern are added.

4. based on the above three points, if we are in. A global variable int I is defined in CC. if you want to use this I in CC, it must be declared as extern int I. If you also write int I, it is repeated definition. So far, I have a question during my learning: if we say that we write the int I code in a code named common. h. CC and B. CC all include, so. CC and B. can I be used without code like extern int I in CC? As a matter of fact, such an idea is extremely wrong after experimentation.

The test code can be constructed as follows: Common. h

Code: select all
#ifndef _COMMON_H
#define _COMMON_H

int i = 10;

void print_sth();

#endif

A. CC:

Code: select all
#include <iostream>
#include "common.h"
using namespace std;

int main()
{
    cout << "i is: " << i << endl;
    print_sth();
    return 0;
}

B. CC:

Code: select all
#include <iostream>
#include "common.h"
using namespace std;

void print_sth()
{
    cout << "i in b.cc is: " << i << endl;
}

Then compile: G ++-o Test A. cc B. CC with the following error:
/Tmp/ccdyjlje. O (. Data + 0x0): Multiple definition of 'I'
/Tmp/ccnz3dvy. O (. Data + 0x0): first defined here
Collect2: LD returned 1 exit status

Why is such an error? The concept is not clear:

A. Think that Conditional compilation can make int I = 10; the code is executed only once. In fact, we use this command line: G ++-e. cc B. CC> & output. The command line is to stop g ++ after preprocessing, print the preprocessing result to the screen, and then open the output file, we will find that int I = 10; this code appears twice. Therefore, the conclusion is that Conditional compilation only takes effect in one source file. In other words, by using Conditional compilation, we can ensure that no extra include is generated in one source file. However, conditional compilation is invalid in multiple source files. Note: For a small knowledge point, do not use the-E Option of G ++ with-O option. Otherwise, the output pre-processing code is incomplete.

Therefore, the above Code is incorrect. Based on the above example, we can summarize some things, and then the fourth point above:

5. in the header file (. in the H file), generally we only write declaration. Therefore, in the header file, we generally do: define types (various struct, typedef, etc.), define functions (as mentioned above, the function declaration without code is Declaration ). To define variables in the header file, you must ensure one of the following two points:

A) This header file will only be included by one source file

B) define the variable as a declaration, that is, add the extern before it, and then define the variable in one and only one source file.

Otherwise, the multiple definition will inevitably occur. To reference a variable defined in another source file or header file, use extern to indicate that the variable is not definition in the current source file. Of course, the premise is that this variable is a global variable (nonsense ).

Therefore, the test example given above can be modified in this way. In common. h:

Code: select all
#ifndef _COMMON_H
#define _COMMON_H

void print_sth();

#endif

A. CC:

Code: select all
#include <iostream>
#include "common.h"
using namespace std;

int i = 10;

int main()
{
    cout << "i is: " << i << endl;
    print_sth();
    return 0;
}

B. CC:

Code: select all
#include <iostream>
#include "common.h"
using namespace std;

extern int i;

void print_sth()
{
    i = 20;
    cout << "i in b.cc is: " << i << endl;
}

In this case, the program is compiled and the output is:
I is: 10
I in B. CC is: 20

Or directly change int I = 10; To extern int I in common. h; then, define ition: int I = 10; In A. CC.

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.