[CMake] Globally add compiler options for gcc

James Bigler bigler at cs.utah.edu
Fri Jan 4 13:14:16 EST 2008


On Jan 4, 2008, at 10:57 AM, Alexander Neundorf wrote:

> On Friday 04 January 2008, James Bigler wrote:
>>> 2008/1/4, Stephen Collyer <scollyer at netspinner.co.uk>:
>>>> This is almost certainly a FAQ but I can't find
>>>> an answer anywhere: I'm building using gcc under
>>>> Linux and VS2005 under Win32. I want to add, say,
>>>> -Wall globally to everything compiled under Linux
>>>> but leave the compile options for Win32 untouched.
>>>
>>> You may put in your main CMakeLists.txt
>>>
>>> IF(UNIX)
>>>     IF(CMAKE_COMPILER_IS_GNUCC)
>>>          ADD_DEFINITION(-Wall)
>>>     ENDIF(CMAKE_COMPILER_IS_GNUCC)
>>> ENDIF(UNIX)
>>>
>>> This example checks for being under UNIX and GCC is the compiler.
>>> If you want to restrict to Linux you should add a test
>>> for CMAKE_SYSTEM_NAME value to match "Linux".
>>>
>>> In this particular case and if you only compile C code
>>> you may replace ADD_DEFINITIONS with:
>>>
>>> SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
>>
>> Doing it this way will most certainly give you grief, unless you  
>> add bit
>> more infrastructure.  First off, every time you run cmake, it will  
>> add
>> another -Wall to your CMAKE_C_FLAGS.  Also, I'm a little unsure of  
>> this,
>> but it may not even set the flags properly without a FORCE in the
>> command.  Also, you will find it hard for a user to turn it off if  
>> they
>> don't wish to see it for some reason.
>
> No, I think you are wrong here, it does work.
> What happens is:
>
> CMAKE_C_FLAGS is read from the cache, e.g. "-O2 -ansi"
> Then the set happens and appends the -Wall, so CMAKE_C_FLAGS is now
> "-O2 -ansi -Wall"
> This new value isn't saved to the cache, but stays only in the  
> "regular" cmake
> variable. To get it in the cache SET(... CACHE ... FORCE) would  
> have to be
> used. Only then it would be appended everytime again.

I had suspisions that this was how that bit of code worked, so thanks  
for clearing this up for me.

There's still the outstanding issue of a flag being added that the  
user can't control (and may get irritated at), but perhaps a new  
cached CMake variable could be added to turn the warnings on and off.

James



More information about the CMake mailing list