I write my project in C using QtCreator as IDE and CMake for build. QtCreator ver. >= 4.0.0 include Clang static analyzer, that I try to use.
In my CMakeLists.txt set:
set(CMAKE_C_FLAGS "-std=gnu99 ${CMAKE_C_FLAGS}")
When I launch analysis in console get errors:
error: invalid argument '-std=gnu++11' not allowed with 'C/ObjC'
How to pass '-std=gnu99' to clang analyzer? Maybe it's hardcoded in QtCreator plugin sources?
UDP1: Seems like it's the QtCreator bug: https://bugreports.qt.io/browse/QTCREATORBUG-16290
The classic approach has been given in the answer by @Petesh.
If you just want to specify the exact C standard version the compiler should comply to, use the global variables CMAKE_C_STANDARD
and CMAKE_C_STANDARD_REQUIRED
.
In your case that would be
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED TRUE)
CMake will then figure out the exact command options for the detected compiler on its own and add them to all invocations of the compiler.
To overwrite this global setting for a specific target, modify the corresponding target properties:
set_target_properties(myTarget
PROPERTIES C_STANDARD 11
C_STANDARD_REQUIRED ON)
An in-depth description of the compile feature options is described in the official CMake documentation: target compile features. Via these compile features it's also possible to require that the detected compiler supports a specific set of language features independent of the language standard:
target_compile_features(myTarget
PUBLIC c_variadic_macros # ISO/IEC 9899:1999
PRIVATE c_static_assert # ISO/IEC 9899:2011
)
The variable CMAKE_C_FLAGS
is for C
code, and not for C++
code. You should be adding it to the CMAKE_CXX_FLAGS
instead.
set(CMAKE_CXX_FLAGS "-std=gnu++11 ${CMAKE_CXX_FLAGS}")
and to retain -std=gnu99
for C code, you would add:
set(CMAKE_C_FLAGS "-std=gnu99 ${CMAKE_C_FLAGS}")