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

Philip Semanchuk ond at semanchuk.com
Wed Aug 26 13:10:45 EDT 2015


On 8/25/2015 11:34 AM, Parag Chandra wrote:
> Hi Philip,
>
> I think I had a similar problem a while back. You basically need to
> initialize CMake’s cache values the first time you run it to create a new
> build system. So something like this:
>
> cmake -C InitialCacheValues.cmake
>
> And then your InitialCacheValues.cmake would contain the extra stuff you
> need to prime the cache with. For example, I do something like this to add
> additional configurations:
>
> set (CMAKE_CONFIGURATION_TYPES
> Debug;Release;RelWithDebInfo;MinSizeRel;Debug_RTLDLL;Release_RTLDLL;RelWith
> DebInfo_RTLDLL;MinSizeRel_RTLDLL CACHE INTERNAL "Configuration types"
> FORCE)
> foreach (cfg "DEBUG" "RELEASE" "MINSIZEREL" "RELWITHDEBINFO")
>    foreach (flag "CXX" "C" "EXE_LINKER" "MODULE_LINKER" "SHARED_LINKER"
> "STATIC_LINKER")
>      set (CMAKE_${flag}_FLAGS_${cfg}_RTLDLL ${CMAKE_${flag}_FLAGS_${cfg}}
> CACHE STRING "${flag} flags for ${cfg}_RTLDLL" FORCE)
>    endforeach ()
> endforeach ()
>
>
> Here you can see that I essentially copy all the important flags from
> existing configurations that CMake is already going to create, and then
> later on in my CMakeLists I adjust them as needed.

Thanks, Parag. I thought what I wanted to do would be almost trivial. 
Your suggestion confirms that it's a little more complicated than I 
expected. I appreciate the input.

Cheers
Philip


>
> On 8/25/15, 2:30 PM, "Philip Semanchuk" <ond at semanchuk.com> wrote:
>
>> 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