[CMake] How to add to CMAKE_C_FLAGS during cache priming?

Philip Semanchuk ond at semanchuk.com
Tue Aug 25 10:30:14 EDT 2015


Hi there,
I'm relatively new to CMake. I'm trying to build a 3rd party library 
under Windows. (The 3rd party library happens to be an old-ish version 
of ITK.) I want to add a compile flag (/bigobj) during the cache priming 
step, but I can't figure out how to do it. Either my changes get 
ignored, or I overwrite the default flags completely which is not what I 
want either. I just want to add my flag to the default flags for all 
compile steps.

Here's what I'm doing: I have a file called prime_cache.cmake, and I 
invoke CMake like so:
cmake -C prime_cache.cmake ..\the_source_code

prime_cache.cmake contains this:
set(COMPILE_FLAGS  /bigobj)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILE_FLAGS}" CACHE STRING "")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS}" CACHE STRING "")

When CMake is done, CMakeLists.txt contains this:
CMAKE_C_FLAGS:STRING= /bigobj

But I want this:
CMAKE_C_FLAGS:STRING= /bigobj /DWIN32 /D_WINDOWS /W3

I've also tried modifying CMAKE_C_FLAGS_INIT, but that didn't affect 
CMAKE_C_FLAGS.

I realize that I can hand-edit CMakeCache.txt to get the flags I want, 
but this is a small part of a much larger process and hand-editing is 
tedious and error-prone so I want to avoid it if possible.

I'm using CMake 2.8.12.2 and would prefer to continue using this version 
even though it's not current.

Thanks for a great tool,
Philip



More information about the CMake mailing list