如何在Qt Creator中为Clang静态分析器设置语言标准(-std) [英] How to set language standard (-std) for Clang static analyzer in Qt Creator

查看:291
本文介绍了如何在Qt Creator中为Clang静态分析器设置语言标准(-std)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用QtCreator作为IDE和CMake用C编写我的项目,以进行构建。 QtCreator版本> = 4.0.0包括我尝试使用的Clang静态分析器。

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.

在我的CMakeLists.txt中设置:

In my CMakeLists.txt set:

set(CMAKE_C_FLAGS -std = gnu99 $ {CMAKE_C_FLAGS})

当我在控制台获取错误:

When I launch analysis in console get errors:

错误:无效的参数'-std = gnu ++ 11'不允许与'C / ObjC'

如何将'-std = gnu99'传递给c分析器?

How to pass '-std=gnu99' to clang analyzer? Maybe it's hardcoded in QtCreator plugin sources?

UDP1 :似乎是QtCreator错误: https://bugreports.qt.io/browse/QTCREATORBUG-16290

UDP1: Seems like it's the QtCreator bug: https://bugreports.qt.io/browse/QTCREATORBUG-16290

推荐答案

@Petesh在回答中给出了经典方法。

The classic approach has been given in the answer by @Petesh.

如果只想指定确切的C标准版本,则编译器应遵守,使用全局变量 CMAKE_C_STANDARD CMAKE_C_STANDARD_REQUIRED

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.

在您的情况下,

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED TRUE)

CMake然后将找出de的确切命令选项

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)






在CMake官方文档中详细介绍了编译功能选项:目标编译功能。通过这些 compile功能,还可以要求检测到的编译器支持一组特定的语言功能,而与语言标准无关:


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
)

这篇关于如何在Qt Creator中为Clang静态分析器设置语言标准(-std)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆