Ensuring fast C++ build speed requires some effort--
Modules should improve the situation, but that is some years off(C++17 if we are lucky).
Modules should improve the situation, but that is some years off(C++17 if we are lucky).
Here is what has worked for me(using Visual Studio)
This is intended for a rapid iteration build, a build you intend to ship would not use these settings.
This is intended for a rapid iteration build, a build you intend to ship would not use these settings.
- Enable /MP Multiprocessor builds.
- Disable Global optimizations
- Incremental Linking (/INCREMENTAL) and Use Library Dependency Inputs(Yes)
- Add /Zc:inline to command line of compiler
- Linker->Optimizations: References(No), COMDAT Folding(No)
- Pre-Compiled Headers(PCH). Put rarely modified files in the PCH, but include everything you can possible include. Test the compilation times of files that use the PCH. You can enable this in Visual Studio(*2). Try to get it as low as possible.
- Limit the headers you include, especially avoid including headers within headers
- Forward declare everything that can be, even types passed by value can be forward declared
- Do not create "registry files". *example: a file containing an enum with an entry for each type of component in your engine. If you do this, you will need to add an entry to this file each time you create a new component, which will result in a large and often triggered rebuild cycle.
- If you find yourself calling from Module A to an object in Module B, but only doing so once, think about wrapping that access in a C function call. The C function can be placed in Module B, then either declared extern within Module A, or a separate header can be created if it is likely that this C function will be called multiple times. This reduces coupling between files. Changes to A will not trigger a recompilation of B.
- /PDBCOMPRESS slows the linker down, should probably not use this
- Use type erasure to reduce coupling & generate less code. For none performance critical paths, a single type erased path can be preferable to a template path per type.
- Focus on optimizing the build speeds of any commonly used templates. There are multiple things you can do here.
- Some templates can be simple wrappers around a none templated type or function, which is itself implemented in a .cpp file, and thus not inline.
- Use variadic templates to replace old style overloads based on # of arguments.
- If there exists a finite set of types that the template can be instantiated with, try explicit template instantiation.
Templates are commonly accused of killing build times.
This is not very accurate, the real culprit is headers. Since templates exist in headers it gives the false impression templates are at fault.
Once you have solved the exponential build time issue caused by headers, templates have a only a small effect on build times.
This is not very accurate, the real culprit is headers. Since templates exist in headers it gives the false impression templates are at fault.
Once you have solved the exponential build time issue caused by headers, templates have a only a small effect on build times.
*2: To enable build timings:
Tools -> Options -> Projects and solutions ->VC++ Project Settings->Build timing->yes
**Tools -> Options -> Projects and Solutions -> Build and Run and set the MSBuild project build output verbosity to "Normal"**