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

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?

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):
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:
How to include compiler flags in the Visual Studio Code debugger?

Related

Error during build of cpp file in VSC: "Errors exist after running preLaunchTask 'C/C++:cpp.exe build active file"

I am unable to run my code and execute the build of the .exe file in the cpp language in Visual Studio Code.
I have downloaded and installed GCC, MinGW software, MinGW-w64 GCC, etc. according to the instructions here and have successfully verified that gcc g++ and gdb are all installed (by checking 'gcc --version' etc in the commad prompt). My intent is to use this compiler to compile my code but it seems I cannot find the correct compiler under the options listed here to simply 'build and debug the active file'.
I also (and perhaps consequentially) have run into a problem with launch.json not being able to build the executable file.
Can someone please help me with this? This is incredibly frustrating and I simply want to be able to run my code. I downloaded MinGW along with gcc is a good compiler for this very purpose.
Image of error message 1: here, to which I click 'debug anyway' and get the next error:
Image of error message 2: here.
Image of launch.json with the configuration that I thought would be appropriate (I found this from another source online): here.
Thank you!!
I recommend deleting all the configurations under the .vscode directory, as they can be regenerated again. After deletion, follow the steps:
Go to your program file, and press F5 to launch debug.
Select g++.exe build and debug active file or a convenient option.
A tasks.json under the .vscode will be generated with the appropriate instructions and the program will be executed instantly.
For example, in my case, it was this:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\TDM-GCC-64\\bin\\g++.exe",
"args": [ // ----- given by me in C/C++ extension settings
"-fdiagnostics-color=always",
"-Wall",
"-O3",
"-pedantic",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

Build multiple C++ files not working - Linux Mint

I am following a C++ tutorial, and the guy shows how to set up tasks.json to run multiple C++ files at the same time, link with timestamp:
https://youtu.be/8jLOx1hD3_o?t=4584
Now I installed the newest versions of all of the packages:
Change my tasks.json to be same as his:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-11 build active file",
"command": "/usr/bin/g++-11",
"args": [
"-g",
"-std=c++20",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/rooster"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": "build",
"detail": "compiler: /usr/bin/g++-11"
}
]
}
Try to compile and it does not work, error:
Starting build...
/usr/bin/g++-11 -g -std=c++20 "/home/elhan/ius/intro to programming/cpp/*.cpp" -o "/home/elhan/ius/intro to programming/cpp/rooster"
cc1plus: fatal error: /home/elhan/ius/intro to programming/cpp/*.cpp: No such file or directory
compilation terminated.
Build finished with error(s).
What might be the problem?
Working dir:
One of the arguments to your build command is
${workspaceFolder}/*.cpp
When variable substitution occurs, VS Code recognizes that there are spaces in your directory name, so the entire argument is enclosed in quotes to protect it from the shell. In the line after Starting build..., you can see the same thing happening with the argument specifying the output destination (which also has spaces after substitution), while the other arguments (with no spaces in them) are passed to the shell without quotes.
"/home/elhan/ius/intro to programming/cpp/*.cpp"
The quotes do indeed protect the argument from being processed by the shell, which is why it is interpreted as a single argument. At the same time, the quotes protect the argument from being processed by the shell, which is why the wildcard * is not expanded. And that's the problem.
If you pay attention to details, the video uses underscores instead of spaces to separate words in directory names. Follow suit, and rename your intro to programming directory to intro_to_programming.

C++ VS Code not recognizing syntax, unable to run code

I am using a specific syntax needed for a course, but when I use this C++ syntax in VS Code, it doesn't work and raises errors.
Here is an example of the syntax that is not working:
error: expected ';' at end of declaration
int i {0};
^
;
When I change it to int i = 0; the error disappears.
It specifically doesn't recognize the {} syntax for setting default variable values. I am using a ssh login for this course and the syntax works well in the ssh, but won't work in VS Code.
I attempted to change my VS Code C++ version to C++17 by doing the top answer in this thread, but it still doesn't recognize the syntax.
Am I using incorrect syntax, or is there a way to fix this?
Adding on to the comment above, this is what solved my issue:
Go to this link: https://code.visualstudio.com/docs/cpp/config-clang-mac
Go to the section Clang on macOS and scroll down to Troubleshooting. Follow the steps in this paragraph:
"If you see build errors mentioning "C++11 extensions", you may not have updated your tasks.json build task to use the clang++ argument --std=c++17. By default, clang++ uses the C++98 standard, which doesn't support the initialization used in helloworld.cpp. Make sure to replace the entire contents of your tasks.json file with the code block provided in the Run helloworld.cpp section."
You will need to copy-paste this exact code and replace the code in the tasks.json folder:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
To find the tasks.json folder, go to your program-file, do Command + Shift + . to find hidden files > .vscode > tasks.json > replace all the code in the file with the one I provided. It updates the C++ compiler to use a more updated version of C++ which recognizes the syntax.

Specify command-line C++ version for cl.exe (Visual Studio Code)

I'm testing the C++17 fallthrough attribute in Visual Studio Code. The IDE has been configured to compile C/C++ code using the Microsoft Visual Studio cl.exe compiler. My task definition (in tasks.json) to build a simple .cpp file in DEBUG mode is:
{
"type": "shell",
"label": "cl.exe: Build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${file}",
"/link",
"/OUT:${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
]
}
This has been tested on several programs and works well. Now I include a switch statement using the new [[fallthrough]]; attribute and the compiler generates:
warning C5051: attribute 'fallthrough' requires at least '/std:c++17'; ignored
Adding "/std:c++17", to the "args" for cl.exe changed nothing (the same compiler warning is generated). Here is the new version:
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"/std:c++17",
"${file}",
"/link",
"/OUT:${fileDirname}\\${fileBasenameNoExtension}.exe"
],
As far as I can see, my syntax is correct according to the Microsoft documentation for specifying the language standard.
What am I doing wrong?
I found this issue while searching for something else, but here's a fix for you :).
Your problem is the order in which you supply the arguments. /Fe: is expecting the file path straight after - https://learn.microsoft.com/en-us/cpp/build/reference/fe-name-exe-file?view=msvc-160
Here's an example args section taken from the VSCode documentation but I've added the /std:c++17 compiler flag
"args": [
"/Zi",
"/EHsc",
"/std:c++17", // <= put your compiler flag here
"/Fe:", // <= /Fe: followed by the path + filename
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
]
Hope this helps, happy coding!

VSCode not recognizing includes from includepath

I am having an issue where VSCode will recognize my include of zipper.h and then out of nowhere flip on me and tell me that there is no such file or directory. I am not sure if this is an issue with my code or includes or vs code.
https://i.gyazo.com/2d35a31abf83546d8633d991bcb4752a.png
https://i.gyazo.com/96ad7825e8d1c390035a4db2f789bbcf.png
I have tried adding it both to my include path and windows environment path. it keeps failing for the same reason. I am very confused on what I'm doing wrong. Is it not recognizing those links? Should I be linking the libraries through g++ when compiling?
#include <zipper.h>
void zipFolder()
{
zipper::Zipper zipFile("logs.zip");
zipFile.add("C:\\Cycling");
zipFile.close();
}
int main(int argc, char const *argv[])
{
return 0;
}
c:\Users\Desk\Desktop\Code\Cycling>cd "c:\Users\Desk\Desktop\Code\Cycling\" && g++ test.cpp -o test && "c:\Users\Desk\Desktop\Code\Cycling\"test
test.cpp:1:10: fatal error: zipper.h: No such file or directory
#include <zipper.h>
^~~~~~~~~~
compilation terminated.
"includePath" property both in c_cpp_properties.json and settings.json relates only to the internal editor's IntelliSense feature and has nothing to do with compilation.
In order to tell the compiler the necessary include paths, you need to specify a correspondent compiler option in your build task (in tasks.json), namely "-Ipath/to/my/include/files".
Here is a build task example from my tasks.json file (look at "args" property - it contains compiler option "-I${workspaceFolder}/../..", i.e. two levels up from the current directory):
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file ver(1)",
"command": "/usr/bin/g++-9",
"args": [
"-std=c++17",
"-I${workspaceFolder}/../..",
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++-9"
}
]
}
You did not tell your compiler anything about a file called Zipper.h or where it is loacted, or anything related to it. "g++ test.cpp -o test" just tells the compiler to compile a source file called test.cpp and link it. You have to understand that Visual Studio Code is not an IDE and can't compile by itself. You should have an file called c_cpp_properties.json file located in your .vscode directory. The one that i use for example looks like this and is configured for mingw64.
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/Source/**"
],
"compilerPath": "C:\\mingw-w64\\mingw64\\bin\\gcc.exe",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
This tells Visual Studio Code where your source files and libraries are. This is what is used for IntelliSense (Syntax Highlights, Error Squiggles, Code Completion, etc). However this has absolutly nothing to do with building your project. Your compiler doesn't now know about the include path's you set in Visual Studio Code. So to compile your project you have to tell your compiler everything he needs to know. Visual Studio Code simply executes what you specify in the task. It's the same as going to that directory and type in the same thing in your command promt. So i recommend you to read up on how to compile a c++ project with g++, your problem is not related to Visual Studio Code at all. If youre planning on doing something thats a bit bigger than just a single source file i strongly suggest you to learn CMake. Compiling by manually calling gcc get's really complicated once you have more source files and includes / libraries to link. Once you have set up your Cmake you can just specify a task in Visual Studio Code similar to this one to build your project:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "cmake --build Build",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I also recommend you to read this:
https://code.visualstudio.com/docs/cpp/config-mingw
This is a really good explanation of basicly exactly what you are trying to do by Microsoft and helped me understanding this when i started to use Visual Studio Code for my c++ work.
Visual Studio Code not changes build command itself, even if includePath changes. You should change build command yourself in .vscode/tasks.json. See this tutorial.