Credit to ajneu in the comments for pointing out this particular variable and its effects.Īnd that’s all it takes to get CMake to set the compiler and linker flags appropriately for a specific C++ standard. The default behavior is for C++ extensions to be enabled, but for broadest compatibility across compilers, projects should prefer to explicitly disable them. This results in modifying the flag used to set the language standard (e.g. Projects will also probably want to set the following too: set(CMAKE_CXX_EXTENSIONS OFF) Note that CMake may still end up selecting a more recent language standard than the one specified (see the discussion of compiler features in the next section). Since you will likely be wanting to disable the fallback behaviour in most situations, you will probably find it easier to just set the CMAKE_CXX_STANDARD_REQUIRED variable to YES instead, since it acts as the default for the CXX_STANDARD_REQUIRED target property. To prevent this fallback behaviour, the CXX_STANDARD_REQUIRED target property should be set to YES. It does not generate an error by default. There is a minor wrinkle in that if the compiler doesn’t support the specified standard, by default CMake falls back to the latest standard the compiler does support instead. It results in adding the relevant compiler and linker flags to the target to make it build with the specified C++ standard. The CXX_STANDARD target property mostly behaves as you would expect. This variable is used as the default for the CXX_STANDARD target property, so all targets defined after the variable is set will pick up the requirement. Valid values for CMAKE_CXX_STANDARD are 98, 11 and 14, with 17 also being added in CMake 3.8 and 20 added in CMake 3.12. The simplest way to use a particular C++ standard in your project is to add the following two variable definitions before you define any targets: set(CMAKE_CXX_STANDARD 11) Happily, with features added in CMake 3.1, it is trivial to handle this in a generic way. If your project targets multiple platforms and compilers, this can be a headache to set up. With the constant evolution of C++, build systems have had to deal with the complication of selecting the relevant compiler and linker flags.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |