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

本秂侑毒 提交于 2021-01-29 20:35:40

问题



I'm working on VS-Code with C/C++ intellisense[gcc-arm]. When I do compile , The VS-Code show me hundred of warning like that:

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

I do not want the VSCode show me those warning. But I have no permission to edit the source code. So, Is the any way to disable those warning by adding some arg to c_cpp_properties.json file?


回答1:


Referring to my own reference document here, if you have access to the build flags, you can pass in -Wno-conversion to disable this warning at compile time.

From my document:

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.

Regarding Visual Studio Code, I do not use that IDE, but the c_cpp_properties.json file appears to have no ability to set build flags: https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference.

The tasks.json file, however, does: https://code.visualstudio.com/docs/cpp/config-linux#_build-helloworldcpp.

Here's their example:

{
  "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
      }
    }
  ]
}

So, it looks like you could add -Wno-conversion to the args list in the JSON file, like this:

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

See also:

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


来源:https://stackoverflow.com/questions/65366489/how-to-disable-warning-from-vs-code-gcc-compiler-not-use-pragma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!