Макрос для использования precompiled headers в CMake + Visual Studio 2008.
macro(USE_PRECOMPILED_HEADER PrecompiledHeader Sources) get_filename_component(HEADER_NAME ${PrecompiledHeader} NAME_WE) set(PrecompiledBinary ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/${HEADER_NAME}.pch) set(PrecompiledSource ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/${HEADER_NAME}.cpp) set(NEED_UPDATE_HEADER_CPP TRUE) set(PrecompiledData "#include \"${PrecompiledHeader}\"") if (EXISTS ${PrecompiledSource}) file(READ ${PrecompiledSource} HEADER_CPP_DATA_IN) if (NOT ${HEADER_CPP_DATA_IN} STREQUAL ${PrecompiledData}) SET(NEED_UPDATE_HEADER_CPP TRUE) else () SET(NEED_UPDATE_HEADER_CPP FALSE) endif () endif () if (NEED_UPDATE_HEADER_CPP) file(WRITE ${PrecompiledSource} ${PrecompiledData}) endif () set_source_files_properties(${PrecompiledSource} PROPERTIES COMPILE_FLAGS "/Yc${PrecompiledHeader} /Fp${PrecompiledBinary}" OBJECT_OUTPUTS ${PrecompiledBinary} ) set(DUPLICATE_SOURCES ${${Sources}}) set_source_files_properties(${DUPLICATE_SOURCES} PROPERTIES COMPILE_FLAGS "/Yu${PrecompiledBinary} /FI${PrecompiledBinary} /Fp${PrecompiledBinary}" OBJECT_DEPENDS ${PrecompiledBinary} ) source_group(src ${PrecompiledSource}) list(APPEND ${Sources} ${PrecompiledSource}) endmacro(USE_PRECOMPILED_HEADER) |
Использование тривиально:
FILE( GLOB_RECURSE SRC *.cpp ) USE_PRECOMPILED_HEADER("precompiled.h" SRC ) |
UPDATE: Окончательный вариант precompiled headers смотрим здесь.