[CMake] What are the best way to set project-wide compiler flags?

Matthew Woehlke matthew.woehlke at kitware.com
Tue May 14 15:15:39 EDT 2013


On 2013-05-14 13:48, Todd Greer wrote:
> My development team and I have struggled with what should be a simple question: what is the best way to add project-wide compiler flags?
>
> Here are the ways I've seen from the documentation and miscellaneous sources:
> 1. add_definitions function
> 2. COMPILE_FLAGS property
> 3. CMAKE_CXX_FLAGS / CMAKE_<lang>_FLAGS cache variable

Personally I use CMAKE_<lang>_FLAGS for this purpose, as does one of my 
coworkers. AFAIK CMAKE_<lang>_FLAGS does apply to all configurations, it 
is the CMAKE_<lang>_FLAGS_<config> that are per-config (and are in 
addition to the non-config-specific flags).

One possible disadvantage to add_definitions is it is more difficult to 
manipulate them later (specifically, there is remove_definitions but I 
don't know offhand how, or if, you can get the current set of flags), 
e.g. if someone downstream from you needs to remove flags you added. 
(That said, my inclination would be to avoid doing this and instead 
provide a variable with the flags you expect.)

-- 
Matthew



More information about the CMake mailing list