首页 > 解决方案 > 如何禁用来自 VS-Code GCC 编译器的警告?(不使用#pragma)

问题描述


我正在使用 C/C++ 智能感知 [gcc-arm] 开发 VS-Code。当我编译时,VS-Code 向我显示了数百个这样的警告:

Conversion from 'int' to u16_t{aka 'short unsigned int'} may change value [-Wconversion]

我不希望 VSCode 向我显示这些警告。但我无权编辑源代码。那么,有什么方法可以通过向 c_cpp_properties.json 文件添加一些参数来禁用这些警告?

标签: c++gccvisual-studio-codeintellisense

解决方案


在这里参考我自己的参考文档,如果您可以访问构建标志,则可以-Wno-conversion在编译时传入以禁用此警告。

从我的文件:

其他 C 和 C++ 构建说明(例如:w/gccclang编译器):

  1. 用于-Wwarning-name打开构建警告“警告名称”,并-Wno-warning-name关闭构建警告“警告名称”。-W打开警告,-Wno-关闭警告。以下是 gcc 对此的看法(来源:https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html;强调添加):

    您可以请求许多带有以 开头的选项的特定警告-W,例如-Wimplicit请求对隐式声明的警告。这些特定的警告选项中的每一个也都有一个否定形式,开始-Wno-关闭警告;例如,-Wno-implicit. 本手册仅列出了两种形式中的一种,以默认形式为准。

关于 Visual Studio Code,我不使用该 IDE,但该c_cpp_properties.json文件似乎无法设置构建标志:https ://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference 。

tasks.json但是,该文件确实: https://code.visualstudio.com/docs/cpp/config-linux#_build-helloworldcpp

这是他们的例子:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++ build active file",
      "command": "/usr/bin/g++",
      "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
      "options": {
        "cwd": "/usr/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

因此,看起来您可以添加-Wno-conversionargsJSON 文件中的列表中,如下所示:

"args": [
    "-Wno-conversion",
    "-g", 
    "${file}", 
    "-o", "${fileDirname}/${fileBasenameNoExtension}"
],

也可以看看:

  1. 如何在 Visual Studio Code 调试器中包含编译器标志?

推荐阅读