Several solutions to reduce Compilation Time

Source: Internet
Author: User
Tags coding standards
[20:27:27 | Author: admin] font size: large | medium | the principle of small include is the most, because the header file is equivalent Code Copy to this file for compilation, and the header file is often used for inclusion by others. Therefore, there are too many project files, and each file has an include chain (including other files ), this chain will not stop at your project, but will extend to all the 3rd-party libraries you use.

A. Remove the include that can be removed.
Note: 1. Some unnecessary header files left over from history are included in your files during code writing. Find them and remove them.
2. Remove the repeated include in the include chain.

B. the header files that can be included in CPP should not be included in the header file.
Note: try to remove the chance of expansion of header files that will be serialized with each CPP.

C. do not include objects that can be declared in the forward direction. The same applies to header files.
Note: In the header file, use the Forward Declaration to save the pointer or reference, and include the header file in the specific implementation of CPP, although it seems that it is in conflict with "Make Header files self-sufficient" in C ++ coding standards. H (included by many classes) can still greatly reduce the Compilation Time, the cost is that the CPP that can be compiled successfully only needs to contain this header file, and some header files need to be included.

For example, the foo Class header file uses the Forward Declaration to save pointers of Class A and Class B or reference them as member variables, only the header files of A and B are contained in the CPP of the foo class. When Class C needs to use the foo class, the header files of the foo class must be included, but the member function of A must be called in the operation, if C does not contain the header file a at the same time, compilation fails.

Although the Code is more complex on the surface, in addition to the benefits of reducing the Compilation Time, the Code is also forcing you to perform decoupling. If you need to include more header files in CPP, it means that the more objects you need to know about this class, you can take a flight to check that your code has no unnecessary coupling, why does this CPP need to know so many details that may belong to other classes .....

D. Put the library files or stable Class header files used by most modules into the pre-compiled header file "stdafx. H ".
Note: Because the include content in the pre-compiled header file is only compile once and is linked multiple times, putting some common classes here will reduce the Compilation Time, but it cannot be messy, the key points are "majority" and "stability". If a header file changes frequently, a small change will lead to rebuild of the entire project, even if it is just a comment, because all CPP files contain stdafx. H and stdafx. H also contains this easily changed header file.

E. pimpl usage
Note: For information about pimpl, you can refer to C ++ coding standards, basically, all protect members are encapsulated using a private forward declarative stuct pointer. basically, it is an ultimate solution, but we have chosen only a few of the most valuable classes to transform our existing architecture, which is too big to be fully implemented, it is intended to be fully applied in other projects in the future.

 

 

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.