How to enforce a specific C++ standard

Answered

Clion (MacOS, every version I have tried) fails to recognize a simple std::shared_ptr in our 100000+ LOC project (which can by the way bring Clion to a grinding halt during code analysis for several minutes -- compared e.g. to Resharper C++ this seems very slow).

To my understanding, Clion parses the CMakeLists.txt and checks the coding standard. Depending on the chosen standard it then parses the code differently.

However, we give users the choice between different C++ standards (currently only C+11 and C++14, we have now deprecated C++03). The choice happens through a Define, which is given to the cmake call. There is no "open" -std=c++11/14 in the CMake code, as the call to cmake actually happens through a wrapper script. It appears as if Clion, when it doesn't understand which standard was chosen, then defaults to some old standard and does not recognize current C++ Standard Library calls. 

Hence, is there a way to force Clion to use a specific standard for parsing ?

Thanks and best Regards,

Ruediger

4 comments

Ruediger,

 

you can enable the desired standard by adding a 

flag to CMAKE_<LANG>_FLAGS variable, where the LANG part specifies the language (C for C and CXX for C++):

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") # enable C99 standard
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # enable C++11 standard
 
Also, you can learn more about using CMake in CLion in our Quick CMake Tutorial: 
0

You can also set the cmake language standard (CMAKE_LANG_STANDARD)

set(CMAKE_CXX_STANDARD 11)

This will set the standard to C++11

1

I have the opposite problem I need to make sure that CLion only uses C++98.

No matter what I try I keep getting clang-tidy for c++11 instead of c++98

I also do not get any red lines or compiler errors when I use syntax for versions greater than C++98

When I set the IDE to C++98 I want the IDE not to let me use any standard greater than C++98

Does anyone know how to enforce this standard in CLion?

 

I have set my CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(myProject)

set(CMAKE_CXX_STANDARD 98)

add_executable(myProject main.cpp Myclass.cpp Myclass.h)

1

Please sign in to leave a comment.