set(CMAKE_CXX_STANDARD 17)įor this reason, stick to the code above when you can! In addition, make sure you call the CMake functions above straight after the project(.) call, or before you create any targets. Therefore, you can fully specify the standard settings like the example below. In other words, if you don’t specify these properties on a target, the default values of these properties will be set as the global variables mentioned above. Specifically, they get initialised to the value of CMAKE_CXX_STANDARD, CMAKE_CXX_STANDARD_REQUIRED, and CMAKE_CXX_EXTENSIONS respectively. Long story short, the CMake documentation specifies that the properties CXX_STANDARD, CXX_STANDARD_REQUIRED, and CXX_EXTENSIONS have initial values. Therefore, the CMAKE_* global variables are very handy here. Unless you have a very good reason, you probably want to set the same standard for every target in your project at once. The Recommended Approach To Set The C++ Standard In CMakeĪlthough you can set the standard property on a “target” basis, essentially being able to have different standards for different parts of your codebase, it’s generally recommended not to. Set_property(TARGET target PROPERTY CXX_EXTENSIONS Off)Īs you can see, we set the standard of targetto 17, requiring that standard, and also turning extensions off. Set_property(TARGET target PROPERTY CXX_STANDARD_REQUIRED On) set_property(TARGET target PROPERTY CXX_STANDARD 17) Setting these properties to targets can be done with the following snippets. For more portable code, set this value to Off to ensure your code will compile in all compilers. Briefly speaking, it tells CMake whether compiler extensions (such as inline assembly) are allowed or not. CXX_EXTENSIONS is another boolean value.Therefore, configuring the project will not succeed if the compiler used does not support the standard you specified if this flag is set to On. CXX_STANDARD_REQUIRED is a boolean that makes the standard defined in CXX_STANDARD a requirement.If you’re reading this from the far future, this list may have expanded. Specifically, at the time of writing the possible values are. CXX_STANDARD dictates which standard version the target will be compiled with.Each target has properties set by CMake, and some of those properties relate to the C++ standard for the compilation: For example, creating executables in CMake will in turn generate a named target for it in CMake. Let’s Talk About The C++ Standard CMake Variablesįirstly, let’s discuss how CMake sets the standard throughout your project.Įssentially, every executable or library you declared in your CMakeSettings.txtis a target. In fact, you only need to set three main variables: CMAKE_CXX_STANDARD, CMAKE_CXX_STANDARD_REQUIRED, and CMAKE_CXX_EXTENSIONS. With CMake, this isn’t as difficult as many think. Setting the C++ standard is just about turning some of these cogs. Figure 1: A nice illustration of the infinite cogs that make up CMake’s usefulness. In this post, we will set the C++ standard with CMake. For this reason, you need to set your compiler’s C++ standards to whatever new version you need. If you’re serious about your coding standards, you’re probably using the latest and greatest C++ features.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |