tags:

views:

339

answers:

1

Hello, 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()?

+1  A: 

I don't believe that add_definitions() adds its arguments to CMAKE_CXX_FLAGS. In fact, as far as I can tell they aren't saved anywhere (apart from arguments beginning with -D or /D which get added to COMPILE_DEFINITIONS).

The simplest way to solve that would be to, whenever calling add_definitions(), to also manually add those flags to CMAKE_CXX_FLAGS.

To see what is in CMAKE_CXX_FLAGS at any point, you can do

message(STATUS ${CMAKE_CXX_FLAGS})

or check the CMakeCache.txt in the build directory (or via ccmake or cmake-gui).

Milliams