Pre-compilation and macro-definition __c++ for C + +

Source: Internet
Author: User
Tags comments data structures

Precompilation is the first step in the entire compilation process and is the result of the g++-e option output.

This step deals with the macros in the source file/header file, which are commonly used in the following categories: file contains: #include macro definition: #define, #undef conditional compilation: #ifdef, #ifndef, #if, #elif, #else, #endif, 1. file contains #include

Preprocessing will include the contents of the file to be included, such as the following file Prepro.cpp:

#include "prehead.h"
int main () {
  Add (1, 2);
}

A header file Prehead.h was introduced:

#ifndef _prehead_h
#define _PREHEAD_H
/* Declaration of a function that add two integer
/int Add (int, int); c7/> #endif

Precompiled with the command g++-e prepro.cpp, the output is as follows:

# 1 "prepro.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "prepro.cpp"
# 1 "prehead.h" 1
  int Add (int, int);
# 2 "prepro.cpp" 2

int main () {
  Add (1, 2);
}

You can see that the macro definitions for the two files in the results are deleted, the comments in the file prehead.h are deleted, and the remainder is introduced into the prepro.h. As to the meaning of the #ifndef final introduction. 2. Macro definition: #define, #undef

#define宏定义的一种用法是声明一个宏 to implement conditional compilation, which is described in conjunction with macro commands such as #ifdef.

#define宏定义的另一种用法是用来定义常量, defines a quasi-inline function. This usage is not good, should avoid use.

Using #define-defined variables, such as: #define PI 3.14, after precompilation all pi will be replaced by 3.14. For example, the last time we talked about the g++ blog:

#define ONE 1
#define TWO 2

int add_one_two () {return one
  + two;
}

The results of precompilation are as follows:

# 1 "add.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "add.cpp"

int add_one_two () {
  return 1 + 2;
}

You can see that one of the definitions is replaced by 1, and the defined two is replaced by 2.

It is good to use constants, but the bad thing about using #define is that variable names are replaced when debugging, and the effect of constants is lost. It is best to define variables in the following way:

Const TYPE Var_name=value

Use #define to define quasi-inline functions, such as the following example (quoted in "Effective C + +"):

#define MAX (A, B) ((a) > (b)? (a): (b))

Generally there is no problem, but:

int a = 5, b = 0;
Max (++a, b);			/* A since the two times * * *
max (++a, B +);		/* B Since the increase of the * *

So this definition is going to go wrong. The purpose of using #define to define functions is to improve the speed of the program, but this way to improve the speed is limited, fundamentally from the improvement of data structures, algorithms to start. This kind of #define, decisive written inline on OK, so many parentheses write tired, look also tired, not necessary. 3. Conditional compilation: #ifdef, #ifndef, #if, #elif, #else, #endif

Conditional compilation is the essence of the application of the macro. First of all to say that the meaning of these instructions, if you have seen my previous blog, and adhere to the Usaco, then you must have been familiar with the most basic C + + grammar, then these instructions to look at a glance can basically know the general meaning. The following is explained in comments:

#ifdef				//if Define, if the definition of ...
#ifndef				//if not define, if not defined ...
#if				//If ...
#elif				/or if ...
#else				//or ...
#endif				//End If
First conditional compilation

Used to prevent a header file from being introduced two times. For example, the header file in the first example prehead.h:

#ifndef _prehead_h/*		if not defined _prehead_h/
#define _PREHEAD_H/		* definition _prehead_h/
* Declaration of a function that add two integer *
/int Add (int, int);
#endif/                         	* end of the corresponding #ifndef * *

Then change our prepro.cpp file, add a line #include "prehead.h"

#include "prehead.h"
#include "prehead.h"
int main () {
  Add (1, 2);
}

Here I start by hand, and it looks like this, and then I analyze it in the comments:

  Then define the *
/* Declaration of a function that add two integer
/int Add (int, int);
#define _PREHEAD_H/*		definition _prehead_h * *  so from here to endif will be removed by the preprocessor/
* Declaration of a function That add two integer *
/int Add (int, int);
#endif/                         	* end of the corresponding #ifndef  ///////////* The following compiler is independent of the second #ifndef _prehead_h

/int main () {
  Add (1, 2);
}

Using the command g++-e prepro.cpp, the output is the same as in the first example, as follows:

# 1 "prepro.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "prepro.cpp"
# 1 "prehead.h" 1
  
   int Add (int, int);
# 2 "prepro.cpp" 2

int main () {
  Add (1, 2);
}
  

Perhaps you would say, this SB, a header file include two times, haha haha. However, this is just an example, the actual project in the introduction of the file is more complex, such as the Prepro.cpp introduced the third file, and then the third file introduced Prehead.h, then there will be no conditional compilation prepro.cpp inside there would be two prehead.h.

Use #ifndef ... #define ... #endif可以保证一个文件只引入一次, here ... Can be anything, as long as you can guarantee that the definition is not defined elsewhere, but the Convention is to define the file name in uppercase and then add a few underscores, like the example above. Second conditional compilation

Used to implement Cross-platform compilation. Let's say this:

#ifdef _linux_
//Linux Platform related code ...
#endif
#ifdef _windows_
//Windows platform related code ...
#endif
#ifdef _macos_
//Mac OS Platform related code
#endif

Then, in the configuration file config.h of an include, if you have the following definition: #define _LINUX_, the compiler compiles only the Linux platform-related code, if you have the following definition: #define _WINDOWS_, Then only the Windows platform-related code is compiled.

Finally, the configuration file config.h is not written manually, but is automatically generated by the script. As for how to write a script or use tools to generate a script, is another topic, and then introduce it later.



Macro instructions There are some other, but with very little, I will not write, want to know friends can refer to this URL: Click to enter. This site C + + documents, manuals, help are first-class, learn C + + can see more.


Reference documents:

Effective C + +, third edition. Scott Meyers. 2005

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.