Visual Studio and GCC precompiled header macro for CMake

Overview

cmake-precompiled-header

Precompiled header setup for CMake. Supported CMake generators:

  • Visual Studio
  • NMake Makefiles
  • Unix Makefiles (GCC)
  • MinGW Makefiles
  • MSYS Makefiles
  • Ninja

Usage

Create a pchheader.{c,cpp} and pchheader.h and add then to the CMake target:

add_library(target ... pchheader.cpp pchheeader.h)

pchheader.h can include all the huge header files that are used everywhere in your project:

#include <string>
#include <iostream>
#include <list>
#include <map>

pchheader.{c,cpp} should just include the header file:

#include "pchheader.h"

In your main CMakeLists.txt, include the macro file:

include(PrecompiledHeader.cmake)

Then add this line, to set up precompiled headers:

add_precompiled_header(target pchheader.h FORCEINCLUDE)

Additional documentation is in PrecompiledHeader.cmake.

Comments
  • Typo in

    Typo in "cmake_parse_arguments(_PCH "FORCEINCLUDE" "SOURCE_CXX;SOURCE_C" "" ${ARGN})"

    Line 79 reads cmake_parse_arguments(_PCH "FORCEINCLUDE" "SOURCE_CXX:SOURCE_C" "" ${ARGN}) Is it a typo that "SOURCE_CXX:SOURCE_C" should be "SOURCE_CXX;SOURCE_C"? Currently it is a colon ":" but it seems that it should be a semicolon “;”

    opened by MonkeyDied 2
  • Adds support for precompiled headers in Ninja builds with Visual Studio targets

    Adds support for precompiled headers in Ninja builds with Visual Studio targets

    The mechanism for the fix is from https://cmake.org/pipermail/cmake-developers/2012-March/015380.html.

    There's also a fix for an issue I ran into where the Windows precompiled header files didn't work out of the box on Linux.

    opened by mwiebe 2
  • Automatic precompiled header for another subproject

    Automatic precompiled header for another subproject

    Main CMakeLists.txt

    add_library (${PROJECT_NAME} STATIC ${HEADERS} ${SOURCES})
    add_precompiled_header (${PROJECT_NAME} ${ALLOCATORS_INCLUDE_DIR}/pch.h 
    ${ALLOCATORS_SOURCES_DIR}/pch.c)
    target_include_directories (${PROJECT_NAME} PRIVATE ${ALLOCATORS_INCLUDE_DIR})
    
    add_subdirectory (tests)
    

    CMakeLists.txt for tests:

    foreach (__TEST__ ${TESTS})
        add_executable (${__TEST__} ${ALLOCATORS_TESTS_DIR}/${__TEST__}.c)
    endforeach ()
    

    When I generate the project for VS2017, I will get precompiled headers for tests(other projects). I don't need use precompiled header in the tests. Is there a solution to this problem?

    opened by VladimirBalun 0
  • Fix issues when changing visual studio config.

    Fix issues when changing visual studio config.

    Now different .pch files are used for debug/release/etc in visual studio projects.

    Before this change switching from debug/release in visual studio would result in lots of pch related errors until you do a rebuild.

    opened by lukedodd 0
  • Rename PrecompiledHeader.cmake

    Rename PrecompiledHeader.cmake

    If you rename PrecompiledHeader.cmake to FindPrecompiledHeader.cmake, then you can use FIND_PACKAGE( PrecompiledHeader REQUIRED ) rather than include...

    opened by towel42-com 0
  • GCC feature

    GCC feature

    GCC pre-compiled header found in different folder (Idea)

    I got gcc pre-compiled headers to work with a regular makefile and tried your code and played with it. The whole forced include is necessary, if one uses #include "header.h. If one uses #include <header.h> instead, the pre-compiled header file can reside in the build folder, if that directory is included as the first one. Even though the documentation says that header.h and header.h.gch must be in the same directory - this is actually not true. I let cmake create a <target>_inc directory, where I let Ninja put the pre-compiled header. The FORCEINCLUDE is no longer necessary.

    opened by matthias-p-nowak 0
  • Release flags are not picked to generate pch file

    Release flags are not picked to generate pch file

    For gcc in release mode, -DNDEBUG flag is missing when pch is generated.

    As the pch is not generated with the same flags, it is not used during the compilation of objects. For example, the following warning appears: precompiled.h.gch/.c++: not used because `NO_INLINE' not defined [-Winvalid-pch]

    Could we have the same flags for the pch generation and the compilation?

    opened by dlaugt 0
  • PCHs outside the current source directory

    PCHs outside the current source directory

    There are projects which have include files outside the current source directory. For example if the project has an include and an src folder, and the current CMake file is in the src folder.

    In these cases, the pch file has to be referenced as ../include/pch.h.

    While this works with the FORCEINCLUDE option (it doesn't without that), it also creates files outside the _pch_binary_dir.

    I think the best approach to resolve this would be the addition of another optional parameter, PCH_PATH, such as:

    add_precompiled_header( target ../include/foo/bar/pch.h PCH_PATH foo/bar/pch.h )
    

    Most likely this parameter should be required if the header path starts with "..", or if it an absolute path.

    This would solve both this issue, and the other I reported.

    opened by dutow 0
  • PCHs in subdirectories doesn't work without FORCEINCLUDE

    PCHs in subdirectories doesn't work without FORCEINCLUDE

    At least for GCC, without the FORCEINCLUDE option, the directory where the pch file is generated is added as an include path.

    get_filename_component(_name ${_input} NAME)
    // ...
    set(_pchfile "${_pch_binary_dir}/${_input}")
    // ...
    list(APPEND _pch_compile_flags -include "${_pchfile}")
    

    This means that if the pch file is in a subdirectory (e.g. "foo/pch.h"), and that directory itself isn't in the include path, the compiler won't be able to find the gch file, and the original header will be used instead.

    opened by dutow 0
Owner
Lars Christensen
Multi-language programmer, system engineer, and tinkerer.
Lars Christensen
CMake precompiled header support via custom PCH compiler extension

CMake precompiled header support via custom PCH compiler extension

Adam Strzelecki 102 Dec 9, 2022
CMake macro to determine the language of a header file

CMake Determine Header Language CMake macro to determine the language of a header file. Status Travis CI (Ubuntu) AppVeyor (Windows) Coverage Biicode

ポリ平方 POLYSQUARE 4 Dec 7, 2018
🍩 A XMake integration in Visual Studio Code

xmake-vscode A XMake integration in Visual Studio Code Introduction A XMake integration in Visual Studio Code. You need install xmake first and a proj

xmake-io 145 Jan 1, 2023
CMake module to enable code coverage easily and generate coverage reports with CMake targets.

CMake-codecov CMake module to enable code coverage easily and generate coverage reports with CMake targets. Include into your project To use Findcodec

HPC 82 Nov 30, 2022
unmaintained - CMake module to activate certain C++ standard, feature checks and appropriate automated workarounds - basically an improved version of cmake-compile-features

Compatibility This library provides an advanced target_compile_features() and write_compiler_detection_header(). The problem with those is that they a

Jonathan Müller 74 Dec 26, 2022
📦 CMake's missing package manager. A small CMake script for setup-free, cross-platform, reproducible dependency management.

Setup-free CMake dependency management CPM.cmake is a CMake script that adds dependency management capabilities to CMake. It's built as a thin wrapper

CPM.cmake 1.6k Jan 9, 2023
CMake checks cache helper modules – for fast CI CMake builds!

cmake-checks-cache Cross platform CMake projects do platform introspection by the means of "Check" macros. Have a look at CMake's How To Write Platfor

Cristian Adam 65 Dec 6, 2022
cmake-font-lock - Advanced, type aware, highlight support for CMake

cmake-font-lock - Advanced, type aware, highlight support for CMake

Anders Lindgren 39 Oct 2, 2022
cmake-avr - a cmake toolchain for AVR projects

cmake-avr - a cmake toolchain for AVR projects Testing the example provided The toolchain was created and tested within the following environment: Lin

Matthias Kleemann 163 Dec 5, 2022
Make CMake less painful when trying to write Modern Flexible CMake

Izzy's eXtension Modules IXM is a CMake library for writing Modern flexible CMake. This means: Reducing the amount of CMake written Selecting reasonab

IXM 107 Sep 1, 2022
[CMake] [BSD-2] CMake module to find ICU

FindICU.cmake A CMake module to find International Components for Unicode (ICU) Library Note that CMake, since its version 3.7.0, includes a FindICU m

julp 29 Nov 2, 2022
A program that automatically generates CMake and Meson configuration files for your Vala project

Autovala is a program and a library designed to help in the creation of projects with Vala and CMake. It also has support for Genie.

Sergio Costas 108 Oct 15, 2022
A template C++ repository, using CMake and Catch

C++ Project Template This is a template project for C++. It uses CMake to build and Catch for unit tests. It is integrated with Travis CI, and builds

Joshua Peterson 49 Oct 23, 2022
Example library that shows best practices and proper usage of CMake by using targets

Modern CMake Sample Sample project that shows proper modern CMake usage on a dummy library and an executable that uses it. Accompanying code to my blo

Pablo Arias 601 Dec 29, 2022
CMake BASIS makes it easy to create sharable software and libraries that work together

CMake BASIS makes it easy to create sharable software and libraries that work together. This is accomplished by combining and documenting some of the best practices and utilities available. This project supplies a fully integrated suite of functionality to make the whole process seamless!

CMake BASIS 44 Nov 28, 2022
This tries to be a minimal cmake example, that covers sources resources dependencies and packaging.

Minimal CMake Example This project tries to be a minimal cmake example. It covers sources, resources, dependencies and packaging. I created this proje

Arne Döring 158 Dec 25, 2022
A simple CMake utility for building and deploying Qt based applications on Android without QtCreator

Qt Android CMake utility What it is When using Qt for Android development, QMake & QtCreator is the only sane option for compiling and deploying. But

Laurent Gomila 225 Dec 2, 2022
A toolchain file and examples using cmake for iOS development

ios-cmake A toolchain file and examples using cmake for iOS development. This is a fork of a similar project found on https://code.google.com/p/ios-cm

Bogdan Cristea 304 Nov 30, 2022
CMake toolchain file and other scripts for the Android NDK

android-cmake CMake is great, and so is Android. This is a collection of CMake scripts that may be useful to the Android NDK community. It is based on

Andrey Kamaev 1.2k Jan 7, 2023