Improving LabWindows/CVI Compile Times with Precompiled Headers
Table of Contents
Introduction
Large projects often consist of multiple source files that include the same header files. The time the compiler takes to process header files over and over again can account for nearly all of the time required to build a project. To make builds faster, LabWindows/CVI 9.0 and later allows users to precompile header files, store the compiled state in a file, and during subsequent compilations, combine the precompiled code with code that is still under development. The first compilation — the one that creates the precompiled header file — takes a bit longer than subsequent compilations, but each subsequent compilation is faster because only modified header files need to be recompiled.
In other words, a pre-compiled header is a cache of header files. The compiler can analyze a set of headers once, compile them, and then have the results ready for any module that needs them.
Using Precompiled Headers
Header files can sometimes contain a very large amount of source code (for instance, the header file windows.h). Instead of compiling header files repeatedly, you can include a prefix header file that references frequently used header files.
The next time LabWindows/CVI compiles the project, LabWindows/CVI compiles the prefix header file first, saves the resulting precompiled header in cvibuild.xxx with the extension .pch, and then compiles the remaining files. In subsequent compilations, LabWindows/CVI loads the precompiled header file instead of the header files listed in the prefix header file.
Complete the following steps to use precompiled headers in a LabWindows/CVI project:
- Create and save a prefix header file.
You also can use the cviincludes.h prefix header file in the cvi\include directory. If you want to make changes to the cviincludes.h file, you must save a copy of the file and make changes in the copy. - Include the prefix header file in the LabWindows/CVI project.
If you use the cviincludes.h file as it is, you do not need to add cviincludes.h to the project. - Enable the Compile with precompiled include file option in the Build Options dialog box.
- Select the prefix header file you want to use from the pull-down menu.
- Click the OK button to save the changes and exit the Build Options dialog box.
Optimizing Precompiled Header Performance
Precompiled code is useful during the development cycle to reduce compilation time, especially if:
- You always use a large body of code that changes infrequently
- Your program comprises of multiple modules, all of which use a standard set of include files. In this case, all include files can be precompiled into one precompiled header.
The relative overhead incurred in writing out and reading back in a precompiled header file is quite small for reasonably large header files. In general, it doesn't cost much to write a precompiled header file even if it does not end up being used, and if it is used, it almost always produces a significant decrease in compilation time. Despite the faster recompilations, precompiled header processing is not likely to be justified for an arbitrary set of files with non-uniform initial sequences of preprocessing directives. The greatest benefit occurs when a number of source files can share the same precompiled header file. The more sharing, the less disk space is consumed. With sharing, the disadvantage of large precompiled header files can be minimized, without giving up the advantage of a significant speedup in compilation times.
Since the amount of compile time you save using precompiled headers depends on the size of the header files you include and the size of the source files, making the best use of precompiled headers will require some experimentation and probably some minor changes to source code.
Organizing Your Code
To get the most benefit from pre-compiled headers in large projects, you should organize your code into multiple libraries/code modules/DLLs, and then configure each library with one set of pre-compiled headers.
In addition, consider the following suggestions when using a prefix header file.
- Adapt the prefix header file to the needs of your project; add and remove header files as necessary. Add “expensive” includes to the pre-compiled header file. “Expensive” headers are the ones that cause a cascade of other includes. Add headers that are included from many different files, even if they’re not very expensive.
For example, if you use a copy of the cviincludes.h file and the file includes header files you do not use in your project, remove those header files to increase the speed of the initial compilation and reduce the amount of time required to load the precompiled header file.
|
|
Note: Do not modify the cviincludes.h file directly. Make a copy of the cviincludes.h file and modify the copy. |
- Don’t put any headers that change frequently in the pre-compiled headers (such as your auto-generated user interface header file). Otherwise, every time you modify a header that was included in the pre-compiled headers file, you’ll cause a full rebuild.
- Keep in mind that certain header files must be compiled in a particular order.
For example, LabWindows/CVI must compile windows.h before all other header files. If you initially do not use windows.h in your application, you might remove that header file from the prefix header file to decrease compilation time. If you remove windows.h, you cannot add it to the source files later because the source files are compiled after the precompiled headers. You must add it back to the prefix header file to preserve precedence and avoid build errors.
Compiler Performance Benchmarks
In addition to support for precompiled headers, the LabWindows/CVI compiler has been improved in LabWindows/CVI 9.0 to remove bottlenecks.
The following graph shows performance benchmarks of nine internal test applications.

These applications were compiled in LabWindows/CVI 8.5, compiled in LabWindows/CVI 9.0, and compiled with LabWindows/CVI 9.0 using precompiled headers.
The following graph shows performance benchmarks of applications submitted by the LabWindows/CVI developer community.

Based on the benchmarks above, most LabWindows/CVI applications will compile faster in LabWindows/CVI 9.0 and later as compared to LabWindows/CVI 8.5, but by using pre-compiled headers, you can maximize your compile time improvements.
Additional Resources
LabWindows/CVI Developer Newsletter
View the latest tutorials, tips, and tricks, suggest upcoming topics, and subscribe to the quarterly newsletter.
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
