Parsing #include and conditional compilation _c language in C + + programming

Source: Internet
Author: User

The role of file inclusion

The so-called "file contains" processing means that a source file can include the entire contents of another source file, which will be included in the document. C + + provides the #include command to implement the "file contains" operation. For example, the following #include commands are available in File1.cpp:

  #include ″file2.cpp″

Its function is shown in the diagram.


The file contains command is useful for saving the duplication of work by program designers.

#include命令的应用很广泛, the #include command is included in most C + + programs. Now, the developer of the library function writes this information in a file, the user simply "contains" the file to come in (such as calling a mathematical function, should contain Cmath file-), which greatly simplifies the program, write a line of #include command is equivalent to write dozens of lines, hundreds of lines or more lines of content. The included file that is commonly used in the header of a file is called a "header file" or "head file".

Header files generally contain the following categories of content:

    • A declaration of a type.
    • function declaration.
    • The definition of a built-in (inline) function.
    • Macro definition. Symbolic constants defined with #define and constant variables declared with Const.
    • global variable definition.
    • External variable declaration. such as Entern int A;
    • You can also include additional header files as needed.

The different header file includes the above different information, provides to the programmer to use, thus, the programmer does not need to write this information by itself, only then uses the line #include command to include this information to this document, greatly enhances the programming efficiency. With the #include command, different files are grouped together to form a file. Therefore, the header file is the interface between the source files.
Two forms of the include command

In the #include command, the file name can be enclosed in angle brackets, as well as double apostrophes. #include命令的一般形式为:
#include < filename >
Or
#include ″ filename ″
Such as:
#include <iostream>
Or
#include ″iostream″
It's all legal. The difference is: when using angle brackets, the system to the system directory to find the file to include, if not found, compile the system to give an error message.

Sometimes the included files may not be in the system directory, and you should use a double apostrophe to indicate the file path and file name in the double apostrophe.

If no absolute path is given in the double apostrophe, such as #include″file2.c″, the file in the user's current directory is default. The system first looks for the files to include in the user's current directory, if it is not found, and then looks in the standard way. If the program to include the user to write their own files, it is appropriate to use the double apostrophe form.

For the header file provided by the system, the included file can be found either in angle brackets or in double apostrophes, but it is obviously more straightforward and more efficient with the form of angle brackets.
about the C + + standard library

In C + + compilation system, many system functions and macro definitions are provided, while the declaration of functions is stored in different header files respectively. If you want to invoke a function, you must include the header file with the #include command. In addition to retaining most of the system functions and macro definitions of C, C + + libraries also add predefined templates and classes. But different C + + library content is not exactly the same, by the C + + compiler system to determine their own. The recent introduction of C + + standard library construction also into the standard, standardized C + + standard library, so that C + + programs can work on different C + + platform, easy to transplant. The header file in the new C + + standard library typically no longer includes suffixes. h, for example:

  #include <string>

But in order to make a large number of existing C programs can continue to use, many C + + compiler system to keep the header file, that is, to provide two different header files, by the program designers selected. Such as:

  Header files in the form of #include <iostream.h>//c
  #include <iostream>//c++


The effect is basically the same. It is recommended to use the C + + standard as much as possible, that is, when you include C + + header files, you do not use suffixes. If the user writes the header file himself, you can use. h as the suffix.

C + + conditional compilation #ifdef #else
In general, compile-time each row in the source program. But sometimes you want a part of your program to compile only when certain conditions are met, which is to specify the conditions for compiling part of the program. If this condition is not met, this part of the content is not compiled. This is "conditional compilation."

Sometimes, you want to compile a set of statements when a condition is met, and compile another set of statements when the condition is not satisfied.

Conditional compilation commands are commonly used in the following forms:
1) #ifdef identifier
Program Section 1
#else
Program Section 2
#endif

The effect is that when the specified identifier has been defined by the #define command, only program Segment 1 is compiled in the program compile phase, or the program section 2 is compiled. #endif用来限定 the scope of the #ifdef command. One of the #else parts can also not be.

2) #if expression
Program Section 1
#else
Program Section 2
#endif

The effect is to compile program segment 1 when the specified expression value is true (not 0), or else compile the program segment 2. A certain condition can be given beforehand to enable the program to perform different functions under different conditions.

For example, when you are debugging a program, you often want to output some of the information you need, and you can insert conditional compilation segments in the source program when you are finished debugging and no longer output the information. The following is a simple example.

#include <iostream>
using namespace std;
#define RUN//When debugging the program, make it a comment line
int main ()
{
  int x=1, y=2, z=3;
  #ifndef RUN//This behavior condition compiles the command
  cout<< "x=" <<x<< ", y=" <<y<< ", z=" <<z<< "\ n"; You need to output this information when you debug a program
  #endif//This behavior condition compiles the command
  cout<< "x*y*z=" <<x*y*z<<endl;
}

Line 3rd The purpose of the #define command is not to use Run to represent a string, but only to indicate that run has been defined, so it does not matter what strings are written behind the run, even the strings are not written. When you debug the program, remove line 3rd (or at first plus//, make it a comment line), because there is no row, so the run definition is not done, and line 6th decides to compile line 7th, and the runtime outputs the X,y,z value so that the user can parse the current value of the variable. Run Program output:

  X=1, y=2, z=3
  x*y*z=6


After debugging is complete, before running, add line 3rd, recompile, because run has been defined at this time, the cout statement is not compiled, so the X,y,z value is no longer exported at run time. The operating situation is:

X*y*z=6

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.