Tuesday Coding Tip 04 - Precompiled headers

posted Originally published at medium.com 1 min read

Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.


Do you use CMake and need to improve your C++ compile times? There’s a technique called precompiled headers. Usually, it is somewhat obtuse to set up, MSVC tends to force you to include the precompiled header in every single .cpp file.

With CMake, you can use a single command to instantly boost your compile times with zero invasiveness. On my hobby project, I reduced compile time down from 3m30s to just 30s (in single threaded CI, I went from 7m to 3m30s).

# PROJECT_NAME refers to target that was used with
# add_library or add_executable command
target_precompile_headers ( ${PROJECT_NAME}
    PRIVATE
        <vector>
        <format>
        <map>
        <unordered_map>
        <array>
        <optional>
        <expected>
        <fstream>
        <filesystem>
        <functional>
        <concepts>
        <memory>
        <string>
        <cstdint>
        <DGM/dgm.hpp>
        <TGUI/tgui.hpp>
        <json.hpp>
)

Just remember to only include headers that don’t change often, presumably never. Good candidates are std headers and third party header-only dependencies.

More Posts

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Tuesday Coding Tip 08 — Explicit template specialization

Jakub Neruda - Apr 21

Tuesday Coding Tip 05 - Object initialization in C++

Jakub Neruda - Mar 31

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelskiverified - Apr 23
chevron_left

Related Jobs

Commenters (This Week)

4 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!