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

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!

Related

Problems getting OpenGL to work in VSCode on Mac

I am trying to complie a basic openGL program in VSCode on mac. I am using glad and GLFW, and I have the Glad files in the same folder as the test.cpp file I am trying to run. However, the include statement throws an error no matter how I type it. This is the program I am writing
#include <iostream>
#include "glad.h"
However, it throws the errors:
screenshot of errors
VSCode cannot find the header files the program needs to run, and the compiler throws errors as a result. I should add, that autocomplete for these libraries is working, however, the file cannot be found by the editor.
Here is the c_cpp_properties.json code
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
Perhaps the problem could be realted to how I am trying to compile the file? Here is my tasks.json file
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Any guidance on how to set this project up correctly, any advice would be greatly appreciated. For reference, here is the tutorial I am trying to follow: https://learnopengl.com/Getting-started/Hello-Window.
Thank you so much!
I'll start by pointing out a few things that aren't really important.
VSCode is not a compiler, nor an IDE. VScode is a text editor. As a text editor it may have some extensions that help you with developing (thus turning it into a make-shift IDE).
What is happening here is that the compiler (gcc) doesn't know where to look for include files. It is even hinting you that you need to update the includePath in a way that you point it to the correct folder with the header files. In the CLI this is done via the -I flag i.e. g++ main.cpp -Ipath/to/your/include_folder/.
If you look into the Getting Started section they even go through these steps and go as far as to suggest the usage of CMake (which is a build tool that helps you generate the makefile with which you build your binaries).
By looking at the tasks.json I can conclude that the extension which you are using in VSCode uses this file to pass down the correct arguments to g++. So the quickest solution for you right now is to specify another element in args that points to the header files i.e.:
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"-Ipath/to/your/include_folder/with_the_headers", <----
"${fileDirname}/${fileBasenameNoExtension}",
],
Hope it helps!

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.

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

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?

Why is "C/C++:" being inserted into the label of my tasks.json file in Visual Studio Code?

I am using Visual Studio Code with MinGW-w64. This involves creating two files (launch.json and tasks.json) that allow me to build and debug my C++ code. I select my tasks.json file by going to Terminal --> Configure Default Build Task... and then selecting "C/C++: g++.exe build active file."
Normally, the tasks.json file appears as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
However, in the last few days, it has appeared as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
When the file is in this format, I'm not able to debug my code. Instead, I get the message "Could not find the task 'g++.exe build active file'.
The only apparent difference is that C/C++is now preceding g++.exe build active file in the "label" line. Once I delete this label, I'm able to get the code to compile and/or debug.
This leads me to ask two questions: first, why is C/C++ being inserted in the label? It doesn't appear in the Microsoft documentation for setting up MinGW with Visual Studio Code: https://code.visualstudio.com/docs/cpp/config-mingw
Secondly, how am I able to permanently remove C/C++ from my label so that I don't have to manually delete it each time? I have tried choosing Configure Task next to C/C++:g++.exe build active file in the Configure Default Build Tasks in the dropdown menu; removing C/C++, and then saving the tasks.json file, but this doesn't appear to cause the build task to change permanently.
Thank you as always for your help.
It took me a while to read the source code of the cpptools extension, and then I found some problems. Under different conditions, the extension has different response
If there is ".vscode" folder in the folder root directory, then when adding the debug configuration, it will only guide the creation of launch.json.The user needs to be guided to configure "tasks.json" again.But the name of the task created at this time is not consistent with value of "preLaunchTask". This is a problem with this extension. I think the reason is that the ".vscode" folder was not found when initializing the debug configuration, some conditions are missing when the extension's code is executed
If there is a empty ".vscode" folder in the root directory of the folder, "launch.json" and "tasks.json" will be created at the same time when initializing the debug configuration.The two files are exactly matched and do not need any modification
If the user configures the task as first.When initializing the debug configuration, the extension will recreate a task called "g++.exe build active file" which is not start with "C/C++".This of course works, but you may need to delete the task created at the beginning whichhas not been used
This design is reasonable because "C/C++: g++.exe build active file" is actually a task template built into cpptools (there are two others). The built-in templates can be referenced by "preLaunchTask", but the user cannot see them in "tasks.json"
If you delete the file "tasks.json" and change the value of "preLaunchTask" to "C:C++: g++.exe build active file".It also works,but this is not a standard usage
I think this is a mistake of them, it started to appear after a recent version

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.