C + + Programming tips reduce compilation time

Source: Internet
Author: User

1. #define的保护
All header files should use # define to prevent header files from being multi-contained (multiple inclusion), naming the format
When is: <project>_<path>_<file>_h_
To ensure uniqueness, the name of the header file should be based on the full path of the source code tree in which the project is located. For example, the header in Project Foo
The file foo/src/bar/baz.h is protected as follows:
#ifndef Foo_bar_baz_h_
#define Foo_bar_baz_h_
...

#endif//Foo_bar_baz_h_

2. Header file dependencies
Use the predecessor declaration (forward declarations) to minimize the number of # include in. h files.
When a header file is included also introduces a new dependency (dependency), as long as the header file is modified,
The code will be recompiled. If your header file contains other header files, any changes to these header files will also cause that
The code that contains your header file is recompiled. Therefore, we would rather include the header files as few as possible, especially those containing
In the other header file.
Using a predecessor declaration can significantly reduce the number of header files that you need to include. Example: The header file uses the class file, but does not
Requires access to file declaration, the header file only needs to be pre-declared class file; no # include
"File/base/file.h".
How does the header file do with class Foo without having to access the definition of the class?
1) Declare the data member type as Foo * or foo &;
2) The function of the parameter and the return value type Foo is only declared (but does not define the implementation);
3) The type of a static data member can be declared as Foo, because the definition of a static data member is outside the class definition.
On the other hand, if your class is a subclass of Foo or contains non-static data members of type Foo, you must
Contains the header file.
Sometimes, a pointer member (pointer members, if SCOPED_PTR is better) is used instead of an object member (object
Members) is indeed more meaningful. However, this approach can reduce code readability and execution efficiency. If only for
Include the header files, or do not replace the good.

Of course, the. cc file requires a defined portion of the class that is used anyway, and it will naturally contain several header files.
Note: You can rely on the declaration to not rely on the definition.


These are the top two requirements for the C + + programming code in Google. The first one believes that we all know. The second is the question I want to say.

Here I use VS2012 to explain the matter. The goal of reducing compilation is to reduce # include;

So let's define two classes first:

#pragma onceclass a{public:a (void); ~a (void);};

#pragma once#include "A.h" class b{public:b (void), ~b (void);p rivate:a A;};

you can see that this is a very simple case of combinatorial classes. there is an object in class B that has a. Let's build a bit below.


At this time a A, the first compilation succeeded

Next we modify object A, and add a private member I,b to object a unchanged ;

#pragma onceclass a{public:a (void), ~a (void);p rivate:int i;};

this time, you can see the following conditions



You can see that after a is compiled. The compiler found that B was imported A.h and then compiled B again.

It's okay to do this when the amount of work is small. but it takes 1 minutes and 2 minutes to compile the code once. We frequently modify the source code and then compile you will crash, isn't it?

So the predecessor declaration is a solution that can be selected.

B's code as long as the following changes, cancel the import A.h in B.h declare class A;

#pragma onceclass a;class b{public:b (void), ~b (void);p rivatea* A;};

we also compile before a does not change.


Also, we compile a private member in a and build it once.


You can see that only the A.cpp file is compiled here.

The reason is that the declaration and definition of C + + is not completely separated. There is no such thing as Java. =




C + + Programming tips reduce compilation time

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.