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

Parag Chandra parag at ionicsecurity.com
Tue Aug 25 11:34:06 EDT 2015


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.

Hope this helps.


Parag Chandra
Senior Software Engineer, Mobile Team
Mobile: +1.919.824.1410

 <https://ionic.com> 

Ionic Security Inc.
1170 Peachtree St. NE STE 400, Atlanta, GA 30309














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
>
>-- 
>
>Powered by www.kitware.com
>
>Please keep messages on-topic and check the CMake FAQ at: 
>http://www.cmake.org/Wiki/CMake_FAQ
>
>Kitware offers various services to support the CMake community. For more 
>information on each offering, please visit:
>
>CMake Support: http://cmake.org/cmake/help/support.html
>CMake Consulting: http://cmake.org/cmake/help/consulting.html
>CMake Training Courses: http://cmake.org/cmake/help/training.html
>
>Visit other Kitware open-source projects at 
>http://www.kitware.com/opensource/opensource.html
>
>Follow this link to subscribe/unsubscribe:
>http://public.kitware.com/mailman/listinfo/cmake


More information about the CMake mailing list