[CMake] setting a CMAKE_CXX_FLAGS without clearing out all the other flags

David Cole david.cole at kitware.com
Thu Nov 29 09:05:35 EST 2007


I do something like this in one CMakeLists file and it works for me:

IF(MSVC)
  #    force treat warnings as errors: /WX
  #    force warning level 4
  #
  FOREACH(lang C CXX)
    IF("${CMAKE_${lang}_FLAGS}" MATCHES "/W[1-3]")
      STRING(REGEX REPLACE "/W[1-3]" "/W4 /WX" CMAKE_${lang}_FLAGS
"${CMAKE_${lang}_FLAGS}")
      MESSAGE(STATUS "Replacing /W[1-3] with /W4 /WX...")
    ELSE("${CMAKE_${lang}_FLAGS}" MATCHES "/W[1-3]")
      SET(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} /W4 /WX")
      MESSAGE(STATUS "Adding /W4 /WX...")
    ENDIF("${CMAKE_${lang}_FLAGS}" MATCHES "/W[1-3]")
  ENDFOREACH(lang)
ENDIF(MSVC)

In the IF clause, I use "STRING(REGEX REPLACE" to do an in-place replacement
of, for example, "/W3" with "/W4 /WX".

In the ELSE clause, I simply use "SET" to append the extra flags.


HTH,
David


On 11/28/07, Jesse Corrington <jesse.corrington at gmail.com> wrote:
>
> Thanks, that works for this specific example. As far as setting other
> flags, is my best bet just to do what I was thinking of removing the flag
> and re-adding it?
>
> On Nov 28, 2007 2:35 PM, David Cole < david.cole at kitware.com> wrote:
>
> > Instead of trying it the hard way (specifically for "/subsystem") why
> > don't you just add "WIN32" to your ADD_EXECUTABLE call?
> >
> > CMake will automatically create a console app for ADD_EXECUTABLE with no
> > flags, and a windows app for ADD_EXECUTABLE with the WIN32 flag...
> >
> > Or were you asking because there is a different use for /subsystem,
> > rather than simply adding WIN32 to your ADD_EXECUTABLE call...?
> >
> >
> > HTH,
> > David
> >
> > On 11/28/07, Jesse Corrington <jesse.corrington at gmail.com> wrote:
> >
> > >  I want to set the flag /subsystem to "windows" for a vc 2005 build,
> > > but I don't want to clear out all the flags and set it. The best way I can
> > > think of to do this is to iterate over the default flags until I get to
> > > /subsystem and remove it and its value, and then add the flag with the value
> > > I want. I just want to make sure this is the best way to handle this. Any
> > > thoughts?
> > >
> > > Thanks
> > >
> > > Jesse
> > >
> > > _______________________________________________
> > > CMake mailing list
> > > CMake at cmake.org
> > > http://www.cmake.org/mailman/listinfo/cmake
> > >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/cmake/attachments/20071129/c7d32fb4/attachment.html


More information about the CMake mailing list