I’m trying to precompile a header file in GCC with the following command:
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/all.hpp.gch
COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} -o ${CMAKE_BINARY_DIR}/all.hpp.gch ${CMAKE_CURRENT_SOURCE_DIR}/all.hpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/all.hpp
COMMENT "Generating precompiled headers"
)
However, I don’t get CMAKE_CXX_FLAGS to expand into flags I’ve set using CMake’s add_definitions(). What is the correct way of compiling in add_custom_command()?
I don’t believe that
add_definitions()adds its arguments toCMAKE_CXX_FLAGS. In fact, as far as I can tell they aren’t saved anywhere (apart from arguments beginning with-Dor/Dwhich get added toCOMPILE_DEFINITIONS).The simplest way to solve that would be to, whenever calling
add_definitions(), to also manually add those flags toCMAKE_CXX_FLAGS.To see what is in
CMAKE_CXX_FLAGSat any point, you can door check the
CMakeCache.txtin the build directory (or viaccmakeorcmake-gui).