如何禁用 VS-Code GCC 编译器的警告? (不使用#pragma)

How to disable warning from VS-Code GCC Compiler? (not use #pragma)


我正在使用 C/C++ intellisense[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 文件添加一些参数来禁用这些警告?

参考my own reference document here,如果您有权访问构建标志,则可以传入-Wno-conversion以在编译时禁用此警告。

来自我的文档:

Additional C and C++ build notes (ex: w/gcc or clang compilers):

  1. Use -Wwarning-name to turn ON build warning "warning-name", and -Wno-warning-name to turn OFF build warning "warning-name". -W turns a warning ON, and -Wno- turns a warning OFF. Here's what gcc has to say about it (source: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html; emphasis added):

    You can request many specific warnings with options beginning with -W, for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning -Wno- to turn off warnings; for example, -Wno-implicit. This manual lists only one of the two forms, whichever is not the default.

关于 Visual Studio 代码,我没有使用那个 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-conversion 添加到 JSON 文件的 args 列表中,如下所示:

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

另请参阅:

  1. How to include compiler flags in the Visual Studio Code debugger?