Can't run c++ build task in VSCode - c++

I have this code in VSCode:
{
"version": "2.0.0",
"tasks": [
{
"taskName": "build first program",
"type": "shell",
"command": "g++",
"args": [
"-g", "firstprogram.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
which is pretty much the exact same code in the official guide to debugging, but it says that g++ is not recognized as a cmdlet, function, script file, or operable program. Pretty much all of the rest of my code is copied from the official thing, aside from code specific to my program, and I have the MinGW c++ compiler.

You need to add the location of the g++ executable to your computer's $PATH Environment Variable.
To do this:
Open up the start menu and search for "Environment Variables".
Click on the "Environment Variables" button at the bottom
Under "System Variables", click on "Path" and then click on "Edit"
Click on "New", and then type the path to your MinGW installation's bin directory. From what you have told me in the comments I believe yours is C:/Users/21nalex/Documents/MinGW/bin

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

Simple C++ program: 'C:\Program' is not recognized as an internal or external command, operable program or batch file

I'm trying to compile a simple C++ program in VS Code -- it's literally just a std::cout statement; I can post it but I can't imagine that's the problem -- I followed the instructions here. The C++ extension is installed and I have Build/Developer Tools for VS17 and 19 - I opened the respective command prompts and they do exist. I even compiled my simple program from the developer command prompt and everything worked fine.
However, whenever I try to build within VS Code after configuring the default build task using Ctrl+Shift+B, I get the 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
error in the output:
I thought it might be because the cl.exe path needed to be the full path so I changed that in both the tasks.json file and the c_cpp_properties.json file to no avail. Whenever I just open a separate cmd window and manually run the compile commands it works fine too.
The thing I'm scratching my head about is why this window looks different than the window that appears whenever I open a new cmd window in VS Code which looks like this:
I don't know if that matters, but again when I manually compile from this window it works.
It seems like I should not be having this many difficulties. I haven't changed anything at all from the instructions but this simple thing isn't working. The only other thing I could think of was maybe it has something to do with the fact I have developer tools for 2017 AND 2019 installed, but to me I can't understand how that would matter.
If it helps, this is my tasks.json file (again, I haven't modified anything from what was automatically generated):
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

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

How to get debugging c++ to work in VSCode on a mac?

Can someone explain how to get building and debugging to work in VSCode on a Mac?
Let's assume we successfully installed cpp tools:
-Including creating a proper task file that works on a mac.
-The required changes to launch.json
-Any other step required.
(Don't get me wrong, I'm not lazy, I have been trying for more then 2 hours now and it seems that a proper answer for this question can help a lot of people.)
Once you have the C/C++ extension downloaded, you can use the configurations to generate a project.json in debug window of VsCode. If you do not currently have a project.json under the project's .vscode folder, hit F5 and a dropdown list should show up. There you can select C++ (GDB/LLDB), and this should generate a project.json for you to use.
If you want to just hit F5 so it automatically compiles and debugs your program, you will need to add a tasks.json. This can be done by hitting F1 and selecting Tasks: Configure Task Runner and select Other. Replace "echo" with "gcc" (or clang) and replace the args with your .cpp files and don't forget to add -g.
You can find more information in their documentation: https://code.visualstudio.com/docs/languages/cpp
I don't count the time I lost looking for an answer to this question!
I found the vscode-lldb extension and it works fine, all other solutions I found don't work for me.
You still have to create configuration files, here are mine to debug my unit tests:
I'm using googletest and extension c++14 in this example
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build & debug tests",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++14",
"-I/src",
"-lgtest",
"tests/MainTest.cpp",
"-o",
"bin/testMain"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
]
}
]
}
launch.json
{
"version": "0.2.0",
"configurations":
[
{
"name": "Debug Tests C/C++",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/bin/testMain",
"args": [],
"cwd": "${workspaceFolder}/tests",
"preLaunchTask": "build & debug tests"
}
]
}

How to build and run C++ code in Visual Studio Code?

I have a tasks.json script that currently compiles the code
{
"version": "0.1.0",
"command": "gcc",
"isShellCommand": true,
"args": ["-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"],
"echoCommand": true,
"showOutput": "always",
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
This works fine, but when I want to run the file I have to run the exe from command line. Is it possible to do this in the task as well? So if it finishes building succesfully it then runs a different task?
If anyone else comes across this when searching like I did, you can now set the property preLaunchTask in your launch.json to your build task's name property and it will run before your launch.
For Example
"name": "Debug (gdb) Launch",
"preLaunchTask": "Build All",
Will run the "name": "Builld All" in your tasks.json before launching your program.
You can read the information on this on the Debugging in Visual Code docs page.
You can configure multiple tasks in Visual Studio Code, one of which will allow you to build your executable, and the other will run your executable.
Optionally, you could also look into Visual Studio Code's "Run Mode" (see here). If you use "Run Mode", you should be able to configure Visual Studio Code to build your executable, and then launch it.
I'm not extremely familiar with "Run Mode", thus I will detail how to define multiple tasks to achieve a similar result.
Disclaimer: Visual Studio Code does not support tasks that use different shell commands (see here).
That's right. At its current state, Visual Studio Code doesn't have "native" support for defining tasks that use different shell commands.
Disclaimer: Visual Studio Code's task-output pane will not allow you to pass input to your program interactively.
If your program relies on user-input (for example, from stdin), you're probably better off not using Visual Studio Code to run your executable.
Basically, what we'll need to do, is define two tasks, one of which will be a build task, the other will be our launch task.
Seeing as Visual Studio Code doesn't have great support for defining multiple tasks that each use different shell commands, we'll need to change our tasks.json's "command" property to cmd (or sh, if on Linux/macOS). We'll also need to set the "args" property to [/C] ([-c] if on Linux/macOS).
The reason behind us doing this, is because we want each of the tasks we're about to define, to be passed as arguments to a new shell instance.
The next step, is to define our build and launch tasks. When we do so, we'll need to make sure we place the command we want to run, as a task argument. For example:
{
"taskName": "build",
"args": ["gcc", "-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"]
}
Finally, what we'll do, is add the "isBuildCommand" property to our build task (and make sure it's true), as well as add the "isTestCommand" property to our launch task (and, again, make sure it's true).
After all of that, our tasks.json file could look something like this:
{
"version": "0.1.0",
"command": "cmd",
"args": ["/C"],
"isShellCommand": true,
"showOutput": "always",
"suppressTaskName": true,
"tasks": [
{
"taskName": "build",
"args": ["gcc", "-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"],
"isBuildCommand": true
},
{
"taskName": "run",
"args": ["${relativeFile}.exe"],
"isTestCommand": true
}
]
}
Note: If placing each task argument in their own string within the args array doesn't work, you can also try placing all of the arguments in a single string within the args array. Example:
["gcc -Wall ${relativeFile} -o ${relativeFile}.exe -pedantic"]
Note: If you would like to be able to invoke your task(s) via keyboard shortcuts, you have the "workbench.action.tasks.build" and "workbench.action.tasks.test" editor commands at your disposal.
If you need an example of binding keys to those commands, here's an example of how I have them mapped in my keybindings.json file:
[
{
"key": "f6",
"command": "workbench.action.tasks.build"
},
{
"key": "f7",
"command": "workbench.action.tasks.test"
}
}
Edit: You probably only need to define a keyboard shortcut for the test task, as the build task probably already has one defined. Check here before you take the time to define a different keyboard shortcut.
You can create a task for build and as the arguments of it you can pass the commands for running. As an example the task I use to compile and run c++ is shown below.
{
"version": "2.0.0",
"tasks": [
{
"label": "g++ build and run",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"out.exe",
"\"${file}\"",
"&&",
"./out.exe",
"<",
"input.in",
">",
"output.out"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
In the above task I compile my source file (the name of the file could be anyone here as ${file} is used) into out.exe and the run out.exe while getting input from input.in and outputting the output to output.out.