Study Notes 17-preprocessing command 3-File Inclusion

Source: Internet
Author: User
I. Basic Concepts

As a matter of fact, we already have access to the file to include this command, that is, # include, which can copy all the content of one file to another.

II. General Form 1. 1st forms # include <File Name>

Go to the directory where the function header file of the C language library is located to find the file.

 

2. 2nd forms # include "file name"

The system first searches for the source code in the current directory. If the source code cannot be found, it searches for the path in the path of the operating system before searching in the directory where the function header file of the C language library is located.

 

Back to Top 3. Usage notes

1. # The include command allows nested inclusion, such as. h contains B. h, B. h contains C. h, but recursive inclusion is not allowed, such as. h contains B. h, B. h contains. h.

The following is incorrect:


 

2. Using the # include command may cause multiple accesses to the same head file, reducing compilation efficiency.

For example:


In one. H, a one function is declared; in two. H, one. H is included, and a two function is declared by the way. (The function implementation is not written here, that is, the function definition)

If I want to use one and two functions in Main. C, and sometimes we do not necessarily know that two. h contains one. H, we may do this:

 

 

After compilation and preprocessing, the main. C code is as follows:

 

1 void one();2 void one();3 void two();4 int main ()5 {6 7     return 0;8 }

 

The first line is composed of # include "one. H "causes 2nd and 3 rows to be caused by # include" Two. H "(because two. h contains one. h ). It can be seen that the one function is declared twice, and there is no need at all, which will reduce the compilation efficiency.

 

To solve this problem of repeatedly containing the same header file, we usually write the header file content as follows:

 


 

Take one for a general explanation. h: for the first time # include "one. H ", because _ one_h _ is not defined, the condition of row 9th is true. Then the macro _ one_h _ is defined in row 10th, and the one function is declared in row 13, finally, compile the condition in 15 rows. When the second time # include "one. H ", because the macro _ one_h _ has been defined before, so the condition of the 9th rows is not true, Jump directly to the # endif of the 15th rows and end the condition compilation. It is such a simple three-sentence code that prevents the content of one. h from being repeatedly included.

 

In this case, in Main. C:

 

#include "one.h"#include "two.h"

 

It becomes:

 1 // #include "one.h" 2 #ifndef _ONE_H_ 3 #define _ONE_H_ 4  5 void one(); 6  7 #endif 8  9 // #include "two.h"10 #ifndef _TWO_H_11 #define _TWO_H_12 13 // #include "one.h"14 #ifndef _ONE_H_15 #define _ONE_H_16 17 void one();18 19 #endif20 21 void two();22 23 #endif

2nd ~ The first line is # include "One. H", 7th ~ The first line is caused by # include "Two. H. After compilation and preprocessing, it becomes:

1 void one();2 void two();

This is what we want.

 

Study Notes 17-preprocessing command 3-File Inclusion

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.