![]() A simple set command will define a variable in the caller’s scope macro(setFoo value) With macros, this is obvious and is often done implicitly. Returning is done via an output parameter. Neither functions nor macros in CMake have a dedicated return channel. This makes macros easier to use when returning a value is necessary. Scope and returning valuesĪs I already mentioned functions introduce a new scope, whereas macros don’t. The little extra effort is worth it, as it could prevent some debugging sessions as the project grows. ![]() ![]() I’d suggest sticking with functions whenever possible. Accidentally overwriting existing variables is also likely. I’d advise against it for the same reason one would avoid macros in C/C++ – it’s best not to pollute the calling scope with whatever variables are defined within the macro’s body. Before we move on to more interesting examples let me touch on one important point.īecause macros don’t introduce new scope they’re often chosen over functions for their ease of use – it’s much more intuitive to “return” a value by defining or mutating a variable in the caller’s scope than it is to use a function. Once defined, both can be called just like builtin CMake commands: myFunction("hello" "functions")Įxecuting this code results in the expected output: $ cmake -S. In the macro’s case they are string-substitutions, but for day-to-day use this shouldn’t make much difference to the user – they’re still accessed the same way within the macro’s body. The parameters foo and bar are regular variables in case of the function. The most important difference between the two is that functions introduce a new scope, whereas macros don’t – their body is essentially copy-pasted into the call site.įunctions and macros can be defined as follows: function(myFunction foo bar) Functions and Macros basicsĪt a first glance, CMake functions and macros behave exactly as one would expect, having experience with any of the C/C++ family of languages. Once the basics are covered I then move on to demonstrating the uses of cmake_parse_arguments. In this post I’ll discuss the basic use of functions and macros in CMake. Due to CMake language scope rules, this requires some special handling and is rather unintuitive, which often causes people to relegate to macros altogether. This is especially true for functions that are expected to return a value. However, they might be somewhat inconvenient to use at first. It enables users to define both functions and macros allowing for encapsulation of repetitive tasks. The value given to option is really only the "initial value" (transferred once to the cache during the first configuration step) and is afterwards meant to be changed by the user through CMake's GUI.Every scripting language needs to provide some facilities for code reuse. But be aware, don't mistake the option with the set command.Mainly just cached strings that only can be ON or OFF and they allow some special handling like e.g.I would recommend to always set cmake_policy(SET CMP0054 NEW) to "only interpret if() arguments as variables or keywords when unquoted." The good news is that this was fixed in CMake 3.1 with the introduction of policy CMP0054.So the best solution here would be - see above - to directly check for if(MSVC).if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") is false, because it evaluates to if("MSVC" STREQUAL "1").set(MyStringWithVar "Some other Text: $" STREQUAL "MSVC") is true, because it evaluates to if("1" STREQUAL "1").When writing CMake scripts there is a lot you need to know about the syntax and how to use variables in CMake.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |