


It covers many key and fundamental concepts of CMake. I recommend reading the book mastering CMake. quote string input with intra-whitespaces to avoid being treated as a list of input strings.quote variable reference to avoid list expansion.don’t use quoted string to refer a constant.don’t use quoted string to refer a variable.set CMP0012 and CMP0054 on so that you will expect CMake works like a normal programming language.Set (VAR a ) # same as set("VAR" "a") message ( $ The unquoted string can be treated as a single string entity (without any whitespace and cannot be parsed as a list) or variable that refers to some string value. Since everything is a string, let’s discuss by its 2 different forms: unquoted and quoted (or string literal, in most languages).

And that’s what policy CMP0054 does if you read the warning carefully. If double-quotes are explicitly used, CMake should recognize the string input to the command is just a string, don’t do any extra interpretations. However, I only want to use "OKAY" as a pure string, and no other meanings neither it should mean a variable. If it’s defined, great, it will be expanded to its value by if command. And CMake engine will check its variable definition map to verify if OKAY is defined. That’s right, everything is string and no explicit double quotes are needed.

Or equivalently, CMake strips the double-quotes. We will cover the warning, and basically, CMake treats if(OKAY) and if("OKAY") as same thing. Users/guihaoliang/Work/CMakeDemo/Okay.cmake: message (Oll Korrect ) Since the policy is not set the OLD behavior will be used. Quoted variables like "OKAY" will no longer be dereferenced when the policy is set to NEW. Use the cmake_policy command to set the policy and suppress this Run "cmake -help-policy CMP0054" for policyĭetails. Policy CMP0054 is not set: Only interpret if () arguments as variables or Users/guihaoliang/Work/CMakeDemo/Okay.cmake: if (OKAY )ĬMake Warning (dev ) at Okay.cmake:5 ( if ): Users/guihaoliang/Work/CMakeDemo/Okay.cmake: set (OKAY Oll Korrect ) Well, you can say everything in CMake is a string, which is similar to Bash and Make. Every command argument is a string and every variable in CMkae is a string. String plays a very important role in CMake. Is there a good rule to avoid the brain-twisting quoted and unquoted strings? I try to give an answer to address this problem. They may be used to store variables persistently across runs. cmake-gui(1) does not show internal entries. cmake-gui(1) offers a text field or a drop-down selection if the STRINGS cache entry property is set.Ī line of text. cmake-gui(1) offers a file dialog.Ī line of text. As the cache entry says:īoolean ON/OFF value. Well, for my understanding, those types are served for cmake-gui purposes. You may see CACHE option can set the type for string, such as BOOL, STRING, and PATH. It sometimes takes a quoted string as variable and sometimes it takes a quoted string as a pure string literal, as many other languages do. Introįor most beginners, CMake if is a nightmare. But I'm not sure if it is sort of common approach when dealing with CMake.ĭoes it make sense to define all variable the CMake submodules needs and provide them in the root CMakeLists.TL DR, check the decision tree and summary. I'm new to CMake and this just seems natural to do things that way. So I require to define project-specific variables that will be used for creation packages using CPack. Set(CPACK_PACKAGE_VERSION_MAJOR $)Īnd the root CMakeLists.txt: set(MYLIB_VERSION_MAJOR 0) Message(FATAL_ERROR "MYLIB_VERSION_PATCH variable must be set") Message(FATAL_ERROR "MYLIB_VERSION_MINOR variable must be set") Message(FATAL_ERROR "MYLIB_VERSION_MAJOR variable must be set") Message(FATAL_ERROR "MYLIB_PACKAGE_NAME variable must be set") It looks likeĬpackMylib.cmake: if(NOT DEFINED MYLIB_PACKAGE_NAME) So I decided to require some variables to be set before including the module.
IMPORTANT CMAKE VARIABLES HOW TO
The problem is the cmake subfiles needs info about how to do their tasks. I'm currently configuring build of my project and decided to split CMakeLists.txt into subfiles which are responsible for a single build sub-task (like testing, packaging, compiling, etc.).
