Header file release in C ++

Source: Internet
Author: User
Tags constant definition

 

The header file release is to release the header file to a specific output directory during the build process of a project, and relies on the code of this project, you need to include the header file from the specific output directory. In other words, in this way, the header file is equivalent to the final generated library/binary file, and it is also an output of the build process.

 

We write C ++ code, which generally includes the required header file directly from the source directory. Why should we use this header file publishing method? We can analyze the possible problems caused by not publishing the header file:

 

Because the header file is directly included in the source directory, we cannot control which header files can be included, but which cannot. In many cases, we may only want to expose APIs of a certain level to users, but it is equally convenient for users to include any header files in the same directory. This significantly increases the possibility of making mistakes.

If the build is incomplete, some problems can only be found at the link.

For example, if A depends on B, you add A new function in B, which is used in. Then you build A directly without building B. This error cannot be detected during compilation, this error is reported only when all source files are compiled and linked. This is a problem for large projects.

If the build is incomplete, some problems can be found at runtime-this is a bug.

For example, if A depends on B, you have modified A macro definition or constant definition in B. For example, you have changed # define PI 3.14 to # define PI 3.1415926, then you forget to build B and directly go to build A. At this time, the compilation is normal, but the definition of PI in Module B and module A is inconsistent, it will inevitably cause runtime problems, which are more difficult to find.

Therefore, to ensure the full consistency of interfaces and binary in C ++ code, including the consistency of visibility and behavior, the use of header file publishing is a very effective method. For the above problems:

 

By releasing only the header files to be exposed to a specific directory, this effectively eliminates the problem of "containing the header files that should not be included ".

Because the updated header file in B has not been released, this issue is found in advance during compilation.

Because the updated header file in B is not released, the PI used in binary files of A and B is 3.14 of the unupdated, thus ensuring consistency.

To release header files, it is actually quite simple. It is mainly about compilation settings and has no impact on source code:

For convenience, A depends on B as an example:

 

When building B, copy the header file of B to the directory at the same level as the output directory bin, such as the include directory.

When building A, use the include directory mentioned above as the include directory

If you use Visual Studio, you can use post build event; if you use gmake, you can add a publish header rule.

 

Of course, if you have a highly intelligent build system, this process can be completely automated. For example, if our team implements one, you only need to specify the dependency relationship and publish the header file, setting the included directories is completed automatically, greatly simplifying build maintenance.

 

Author lzprgmr

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.