larsch / cmake-precompiled-header Goto Github PK
View Code? Open in Web Editor NEWVisual Studio and GCC precompiled header macro for CMake
Visual Studio and GCC precompiled header macro for CMake
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.
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?
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 โ;โ
This is because with Ninja generator 'CMAKE_CFG_INTDIR' get resolves to '.' thus making all projects pointing to the same file which at this point in time it is not supported. one can use CMAKE_CURRENT_BINARY_DIR instead.
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?
setting 'CMAKE_CXX_STANDARD' is not being applied to the compiler flags. It causes compilation to fail when using c++11 or greater features.
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.
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?
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...?
Adding "IMPLICIT_DEPENDS CXX ${_pch_header}" in addition to "DEPENDS ${_pch_header}" fixed it for me. LucidFusionLabs@27eda54 is the fork I'm using. Thanks!
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.
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}"
DEPENDS "${_pchfile}" "${_pch_flags_file}"
COMMENT "Precompiling ${_name} for ${_target} (C++)")
add_custom_command(
OUTPUT "${_output_c}"
COMMAND "${CMAKE_C_COMPILER}"
DEPENDS "${_pchfile}" "${_pch_flags_file}"
COMMENT "Precompiling ${_name} for ${_target} (C)")
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 you rename PrecompiledHeader.cmake to FindPrecompiledHeader.cmake, then you can use FIND_PACKAGE( PrecompiledHeader REQUIRED ) rather than include...
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.