CMake Version setting: Am I suppose to set the `C++` version in my project-root `CMakeLists.txt` file using CMAKE_CXX_STANDARD, or `CXX_STANDARD`?

Viewed 101

This is probably a dumb question. If its a duplicate I couldn't find it.

I am pretty new to CMake, I was first introduced to it via JetBrains IDE: CLion. I moved to VSCode, because I don't feel as comfortable, or as well equipped in any other environment, as I do in VSCode. Unfortunately building projects is generally easier in a true IDE, especially in VS2022. In VSCode I have to configure most stuff myself, but in truth, I learn way more doing that way, so I don't mind. So I usually set the version of C++ I am using via the project-root: CMakeLists.txt. Just now when I went to start a new project though I noticed something that popped up from Intellisense/Intellicode, there were two options I could choose:


  1. CMAKE_CXX_STANDARD <ver>

  2. CXX_STANDARD <ver>


I have used, CMAKE_CXX_STANDARD 17, for about a year and a half, and never seen the version of the setting that isn't prepended with "CMAKE_". When I google it, I just find results for the CMAKE_CXX_STANDARD setting, which isn't surprising considering there syntax is nearly identical.

Further more, I tried them both, and I didn't notice a difference, but I don't really notice a difference when I don't add the configuration anyways. I was just wondering if what the difference is, if any, so I know which to use, and more importantly why to use it.



Below is an img that shows the C_CPP Microsoft IntelliSense extension's suggestions in V.S. Code.

enter image description here




NOTE:

The line: set(CXX_STANDARD) is formatted wrong, it should have looked like this instead: set(CXX_STANDARD 17)

1 Answers

You could use either of CMAKE_CXX_STANDARD and CXX_STANDARD, but note that they are different entities:

  1. CMAKE_CXX_STANDARD is a variable, so it can be set with set command:

    set(CXX_STANDARD_REQUIRED 17)
    

    This setting would affect on all executable/library targets created after it.

  2. CXX_STANDARD is a target property, so it can be set with set_property or set_target_properties commands:

    set_property(TARGET my_target PROPERTY CXX_STANDARD 17)
    

    This setting should be performed after creating the target my_target and affects only on the given target.


Attempt to set a property like a variable:

set(CXX_STANDARD 17)

is simply wrong: It sets a variable CXX_STANDARD, and this variable is never used by CMake.

Related