

#Cmake generator expression code
The above code therefore doesn’t do what you might expect. Consider the scenario where a custom target should log a configuration-dependent message: # WRONG: Spaces not handled correctlyĬMake parses arguments by whitespace before it tries to identify generator expressions. When the generator expression does contain a space, new line or other whitespace, you must exercise more care. The above uses a very simple generator expression which contains no spaces. The following example defines a build target PrintHash which prints the MD5 hash of an executable’s binary file: add_executable(MyApp. Common examples include custom commands or as values for some target properties. A generator expression has the form $ and can be used in a variety of places. These typically arise from a misunderstanding of CMake’s quoting rules and argument processing. In the CMake forum, you will sometimes see posts asking why CMake doesn’t seem to recognize a particular generator expression. Message("With evaluation of $") Number of arguments: 1įor a more detailed discussion of passing arguments using variables, see Forwarding Command Arguments In CMake.

Set(args something="unexpected perhaps" anotherArg) # Relies on legacy behavior, don't do this You should avoid using unescaped quotes anywhere other than the start and end of a value. CMake’s documentation warns that this is legacy behavior. The quotes do still prevent spaces from acting as argument separators. If a value does not start with double-quotes, any double-quotes after the first character become part of the value. Some languages support quoting that starts part way through a value, but CMake’s handling of such arguments is different to most.

add_library(MyThings STATIC someFile.cpp "I need quotes.cpp") Directory separators do not require quoting. However, you only need quotes if the file name contains whitespace or semicolons (and file names should never contain semicolons, since they would almost certainly lead to other problems). To demonstrate, the following are all equivalent: add_library(MyThings STATIC someFile.cpp)Īdd_library(MyThings STATIC "someFile.cpp")Īdd_library("MyThings" "STATIC" "someFile.cpp")ĭevelopers often feel the need to treat file names and paths specially, often adding quotes when not required. It is up to the command’s implementation to give those strings meaning as keywords. We might think of those as being special, but from CMake’s point of view, they too are just strings. In fact, much of the time, you can pass strings to commands without quoting. Unlike many scripting languages, CMake doesn’t always require strings to be quoted. Command Arguments From Substituted Content.
