CMake questions (add_definition, CMP0063)

Alex Merry alex.merry at kde.org
Wed Oct 14 10:02:31 UTC 2015


On 2015-10-05 14:30, Frederik Schwarzer wrote:
> Am Samstag, 3. Oktober 2015, 00:30:04 schrieben Sie:
>> On Fri, Oct 2, 2015 at 11:09 PM, Frederik Schwarzer
> <schwarzer at kde.org> wrote:
>> > while looking through some make files, I found two things that I
>> > do
>> > not understand.
>> >
>> > 1)  add_definitions("-Dx -Dy") vs. add_definitions(-Dx -Dy)
>> > Is there a difference? Laurent added the quoted version in KShisen
>> > but "in the wild" I can only find the non-quoted version.
>> 
>> Well, not that I know of. If anything I would suggest /not/ using
>> the quotes, without knowing Laurent's reasoning. Maybe you can ask
>> him and enlighten us?
> 
> Unfortunately he wasn't as talkative as I had hoped. :)
> He said that he does not understand my problem since both versions
> worked. Well, he is right but my curiosity was not satisfied that day.
> :)

There is a difference, and the second form is the correct one (although 
it makes very little practical difference, at least on most UNIX 
systems).

In CMake's internal language, the quoting works pretty much like on the 
command line -- with add_definitions("-Dx -Dy"), add_definitions is 
being passed a single argument "-Dx -Dy", and in add_definitions(-Dx 
-Dy) it is being passed two arguments, "-Dx" and "-Dy". You could 
equivalently write add_definitions("-Dx" "-Dy").

The difference is that when add_definitions receives "-Dx" as a single 
argument, it recognises it as a definition declaration and munges it if 
necessary. In particular, with MSVC it will produce "/Dx" (it also 
attempts some escaping magic if necessary, as with "-DFOO=\"some 
string\""). It also gets stored internally as a "definition" argument 
for the compiler.

When it receives "-Dx -Dy", it doesn't recognise it as a definition 
declaration, and assumes you want to dump it wholesale on the compiler 
command line. It gets stored internally as a compiler "flag", and 
doesn't get munged.

You may wonder why "-Dx -Dy" works at all -- why doesn't this get passed 
as a single argument to the compiler, and then get rejected for not 
being a valid argument? Well, for compatibility reasons, CMake stores 
all the "compiler flags" added this way as a single string, and just 
appends each new one with a space separator and without attempting any 
escaping. Then the shell / build tool uses the spaces to tokenise the 
flags.

Incidentally, if you do want to add actual flags, you should use 
add_compile_options() instead.

Alex


More information about the Kde-buildsystem mailing list