Giter Site home page Giter Site logo

cmake-precompiled-header's Introduction

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.

cmake-precompiled-header's People

Contributors

larsch avatar lukedodd avatar mwiebe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cmake-precompiled-header's Issues

Avoiding using the same pre-compiled header in multiple projects/targets

As noted in this CMake script's caveats:

  • Its not currently possible to use the same precompiled-header in
    more than a single target in the same directory (No way to set
    the source file properties differently for each target).

However, I'm wondering if anyone has found a way to re-direct a secondary projects awareness of the cxx_stdafx.pch file this CMake script creates? I share source code across two projects in a single Visual Studio solution, and I get errors related to the generated pre-compiled header file, despite the secondary project specifically set to not use pre-compiled headers.

My functional, but clumsy, work-around is to duplicate the shared source so each project files are entirely separate. Has anyone else had to deal with this issue...?

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.

Rename PrecompiledHeader.cmake

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

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?

Not all flags are getting grabbed.

setting 'CMAKE_CXX_STANDARD' is not being applied to the compiler flags. It causes compilation to fail when using c++11 or greater features.

NMake rebuilds everything when used with add_subdirectory()

Using the add_subdirectory function in cmake to include a project using a precompiled header will work with VS/MSBuild generators, but the NMake generator will not detect its output binaries and will always do a complete rebuild.

Rebuilding does not happen when you disable precompiled headers.

Maybe this is a problem with the NMake generator itself?

-g flag not picked up for PCH compile

For some reason, the -g flag is not in CMake's $<TARGET_PROPERTY:xxx,COMPILE_FLAGS> nor
...COMPILE_OPTIONS, and so doesn't get written to the .rsp file.

This results in a warning of different options for pch and regular source compile.

Temporarily adding -g to the export_all_flags function fixed the warning.

Do you know Is there some other CMake property for the debug flag?

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?

If project define string difinition or have debug compiller options - not work precompile header.

if some in CMakeLists.txt have :
target_compile_definitions(test-cxx-force PUBLIC OPENFLAG="Open")
build wrong gch file.
if call cmake with -DCMAKE_BUILD_TYPE=Debug
build wrong gch file.
Try this lifehack :)

#export_all_flags("${_pch_flags_file}")
#set(_compiler_FLAGS "@${_pch_flags_file}")
add_custom_command(
OUTPUT "${_pchfile}"
COMMAND "${CMAKE_COMMAND}" -E copy "${_pch_header}" "${_pchfile}"
DEPENDS "${_pch_header}"
COMMENT "Updating ${_name}")
add_custom_command(
OUTPUT "${_output_cxx}"
COMMAND "${CMAKE_CXX_COMPILER}" $(CXX_DEFINES) $(CXX_FLAGS) -x c++-header -o "${_output_cxx}" "${_pchfile}"
DEPENDS "${_pchfile}" "${_pch_flags_file}"
COMMENT "Precompiling ${_name} for ${_target} (C++)")
add_custom_command(
OUTPUT "${_output_c}"
COMMAND "${CMAKE_C_COMPILER}" $(C_DEFINES) $(C_FLAGS) -x c-header -o "${_output_c}" "${_pchfile}"
DEPENDS "${_pchfile}" "${_pch_flags_file}"
COMMENT "Precompiling ${_name} for ${_target} (C)")

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.

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.

when using MinGW target in Cmake it can't be compiled

adding PCH is working when using Visual Studio target but when I switch to MinGW target it is showing error:

mingw32-make[2]: *** No rule to make target '../CoreLib.h', needed by 'CoreLib_pch/CoreLib.h'. Stop.
mingw32-make[1]: *** [CMakeFiles/CoreLib.dir/all] Error 2
mingw32-make: *** [all] Error 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.