[CMake] Overriding C compiler flags in CMakeLists files

Michael Hertling mhertling at online.de
Tue Aug 9 11:28:50 EDT 2011


On 08/09/2011 04:43 PM, Michael Wild wrote:
> Well, directory properties are used by _targets_ defined in that
> directory. It doesn't matter where the sources are located.
> 
> Michael

Additionally, setting a source property in the leaf CMakeLists.txt
files as it has been suggested in this thread earlier wouldn't work
also since such properties apply to source files for targets defined
in the same CMakeLists.txt only. As the target is defined in the top-
level CMakeLists.txt, any source property set in a subdirectory's
CMakeLists.txt won't take effect.

To revisit Andrei's concern: As your target is defined in the top-level
CMakeLists.txt, only settings which are valid in this file apply to the
My_Exec target, i.e. they must be defined in this CMakeLists.txt since
there's no superior one to inherit from. Thus, you should imply source
file properties, e.g. COMPILE_FLAGS, on the source files from within
the top-level CMakeLists.txt. As an alternative, in each of your sub-
directories, you might define a static library target built from the
respective source files and link your top-level target against these
libraries, provided your toolchain supports it. In this manner, you
can define fine-grained target/directory-specific compilation flags,
and linking against a static library is usually quite the same as
incorporating the library's object files immediately.

'hope that helps.

Regards,

Michael

> On 08/09/2011 04:36 PM, Tim Gallagher wrote:
>> Interesting -- I guess this is sort of off topic from the original post, but that brings up a question:
>>
>> How does setting the COMPILE_DEFINITIONS on the directory work then? I was under the impression (not having tried it) that those definitions would only be added when compiling files contained in the directory. I assumed COMPILE_FLAGS could, if it existed, operate the same way. 
>>
>> Tim
>>
>> ----- Original Message -----
>> From: "Michael Wild" <themiwi at gmail.com>
>> To: cmake at cmake.org
>> Sent: Tuesday, August 9, 2011 10:32:58 AM
>> Subject: Re: [CMake] Overriding C compiler flags in CMakeLists files
>>
>> On Tue 09 Aug 2011 04:28:51 PM CEST, Tim Gallagher wrote:
>>> In your leaf CMakeLists.txt, try doing:
>>>
>>> set_source_files_properties(${SOURCE_FILES} PROPERTIES COMPILE_FLAGS
>>> ${LOCAL_COMPILE_FLAGS})
>>>
>>> where ${SOURCE_FILES} is a list of the files in the directory and
>>> ${LOCAL_COMPILE_FLAGS} is the list of flags for those files.
>>>
>>> Ideally you would want to set this for the entire directory, but
>>> according to the documentation, the COMPILE_FLAGS property doesn't exist
>>> for directories.
>>>
>>> Tim
>>>
>>
>> Setting a directory property wouldn't help, since the directory 
>> property is inherited by the target, not the source files. And since 
>> the target lives in the top-level CMakeLists.txt file, the directory 
>> properties wouldn't come into play at all.
>>
>> Michael


More information about the CMake mailing list