Tuesday Coding Tip 04 - Precompiled headers

●5 ●34 ●98
calendar_today • schedule1 min read
— Originally published at medium.com

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.

Want more #programming & #cpp deep dives?

Join 6,093+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 4 of 32 in Tuesday Coding Tips

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

Let's play with C++ reflection

Jakub Neruda - Nov 12

Tuesday Coding Tip 17 — Wrapping C APIs

Jakub Neruda - Jun 30

Tuesday Coding Tip 31 — Deducing this

Jakub Neruda - Nov 17
chevron_left
2.1k Points • 137 Badges
Brno, Czech Republic • linkedin.com/in/jakub-neruda
43Posts
38Comments
25Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.

Commenters (This Week)

39 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!