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

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.

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

How to instantiate variables using brackets on visual studio code?

Following up from here, I understand that g++ uses c++03 syntax by default, however how do I go about changing this to c++11 on VSC?
My best guess is that I change my task.json file, but how exactly?
Tasks.json file:
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "/usr/bin/clang++",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "silent"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
I'm also using VSC on Mac.

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?

VS code cannot compile mutiple C++ files(WSL)

I just started to setup my VS Code in WSL. I followed by the instruction from GCC on Windows subsystem for Linux. Most of the work is done, there is only one issue. For this part:
Modifying tasks.json
You can modify your tasks.json to build multiple C++ files by using an argument like ${workspaceFolder}/*.cpp instead of ${file}. You can also modify the output filename by replacing ${fileDirname}/${fileBasenameNoExtension} with a hard-coded filename (for example 'helloworld.out').
I want to compile multiple cpp files, but the method provides from above ${workspaceFolder}/*.cpp instead of ${file} doesn't work for me. After I replaced it, the g++ seems like recognize the *.cpp as a file so it can't find it.
This is what the terminal shows.
g++: error: /.../.../*.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
The terminal process terminated with exit code: 1
I find out the problem is on the name of my folder. There is a space in my folder name, so change the name of the folder solved this problem. It is really not a good habit to use space. But if you continue to want to use a name that contains space, do "\"${workspaceFolder}\"/.cpp" instead of "${workspaceFolder}/.cpp".
Try this instead: "${workspaceFolder}/**.cpp"
From the website you posted the example tasks.json would be this
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": ["-g", "${workspaceFolder}/**.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I can't tell you the reasoning behind the double asterix * but it takes all the files with .cpp ending in the current folder.

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.