Extern, header file and ifndif macro

Source: Internet
Author: User

# Include can contain declarations of variables and functions in other header files. Why do I need the extern keyword? If I want to reference a global variable or function, I only need to include # include <XXX. h> (XXX. h contains the statement of a.) Is that okay? Why use extern ?? This problem has always been a real problem that has plagued me for many years. Today, I checked the Internet and finally got a small profit:

Header file

First, let's talk about the header file. In fact, the header file has no effect on the computer. She just expanded it at # include during pre-compilation, which makes no difference, in fact, the header file is mainly for others.

I did an experiment, changed the suffix of the header file to xxx.txt, and then used

# Include "xxx.txt"

Compilation and links have passed smoothly. We can see that the header file only serves to read the code and has no other functions!

Whether it's C or C ++, you put your functions, variables, struct, and classes in your. C or. cpp file. Compile it into Lib, DLL, OBJ,. O, and so on. Then, when others use it, the most basic GCC hisfile. cpp Yourfile. o | OBJ | DLL | lib and so on.
But for our programmers, how do they know what is in your Lib, DLL? It depends on your header file. Your header file is a description of the user. Descriptions of functions, parameters, and various interfaces.
Since this is a description, the header file contains the "Declaration" of functions, variables, and classes. Remember, it is a "Declaration", not a "Definition ".
Then, let me assume that you know the differences between the Declaration and definition. Therefore, it is best not to define something in the header file. For example, global variables:
/* XX header file */
# Ifndef _ XX _ header file. h
# DEFINE _ XX _ header file. h
Int;
# Endif

In this case, int A is a global variable, so if this header file is referenced multiple times, your A will be repeatedly defined.
Apparently, the syntax is wrong. However, with this # ifndef Conditional compilation, you can ensure that your header file is referenced only once, but it may be wrong, however, if multiple C files contain this header file, errors will still occur, because the macro name is only valid for the current C source file, so there will be no errors during compilation of these multiple C files, however, an error will be reported during the link, saying that you have defined the same variable in multiple places,

Linking...
Incl2.obj: Error lnk2005: "int GLB "(? GLB @ 3ha) already defined in incl1.obj
Debug/incl.exe: Fatal error lnk1169: one or more multiply defined symbols found

Note !!!

Extern

This keyword is really hateful. During the Declaration, this extern can be omitted, so you may not know whether it is declaration or definition. The following two types are divided into variables and functions:

(1) variables

Especially for variables.
Extern int A; // declare a global variable
Int A; // defines a global variable.

Extern int A = 0; // define a global variable A and give the initial value.
Int A = 0; // defines a global variable A and returns the initial value,

The fourth is equal to the third, which defines a global variable that can be used externally and returns the initial value.
Confused, they look really like. But the definition can only appear in one place. That is to say, either int A; extern int A = 0; or int A = 0; can only appear once, and that extern int A can appear many times.

When you want to reference a global variable, You need to declare, extern int A; at this time, extern cannot be omitted, because if it is omitted, it becomes int A; this is a definition, not a declaration.

(2) Functions
The same is true for functions. They are also definitions and declarations. When defined, use extern, indicating that this function can be referenced externally. When declared, use extern to indicate that this is a declaration. However, because the definition of a function is different from that of a declaration, the definition function must have a function body, and the declaration function does not have a function body, so the extern can be omitted during function definition and description, other files also know that this function is defined elsewhere, so it can be done without the extern. The two are so different that extern is omitted.
For example:
/* A CPP file */
Int fun (void)
{
Return 0;
}

Well, we have defined a global function.
/* Another CPP file */
Int fun (void );
We made a statement on it, and then we can use it later.
The addition of extern is the same.
We can also put the statement of fun in a header file, and finally it becomes like this
/* Fun. H */
Int fun (void); // function declaration, so extern is omitted. The complete values are extern int fun (void );
/* Corresponding fun. cpp file */
Int fun (void)
{
Return 0;
} // A complete global function definition. Because of the function body, extern is also omitted.
Then, a customer, a customer who wants to use your fun, will include this header file, OK, and a global declaration. No problem.
However, if the customer wants to use a global variable, the extern variable must be used. Otherwise, the definition is defined.

Summary:

For a variable, if you want to use the variable of another source file in this source file, you need to declare the variable with extern before use, or declare the variable with extern in the header file;

For a function, if you want to use a function of another source file in this source file, you need to declare the variable before use. It does not matter if the declared function is added without the extern, therefore, you do not need to add extern to the function in the header file.

 

 

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.