Building a C++ Program in Visual Studio Code - c++

To my knowledge, I have followed all of the steps to build a C++ program in Visual Studio Code on Windows 10. I have gcc-7.1.0-64 installed under C:/MinGW, the C/C++ extension installed in VS Code, and have configured a build task for my HelloWorld.cpp.
The issue:
When I try to build the program by opening the Command Palette and then typing Tasks: Run Build Task, It displays the error:
No Build Task found. Press 'Configure Build Task' to define one.
despite the fact that I have already done exactly that. I am probably missing something simple, but no tutorial or documentation I could find anywhere explains how to make this work. Any help is appreciated.
HelloWorld.cpp
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello world\n";
}
c_cpp_properties.json (irrelevant Mac/Linux setup omitted from snippet)
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:/MinGW/include/c++/7.1.0/*"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceRoot}",
"C:/MinGW/include/c++/7.1.0/*"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 2
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "HelloWorld.cpp",
"command": "c++",
"args": ["-g", "HelloWorld.cpp"],
"type": "shell"
}
]
}

Thanks to #RobLourens's comment for the answer.
The easiest way for tasks 2.0 is, run "Configure default build task", which will let you pick the build command, and set the group property for you.
I soon realized that it adds a line under the "group" section of the specified build task in tasks.json: (may be added manually to circumvent the formal method)
"isDefault": true,

Just add "isBuildCommand": true inside your task.

Related

Visual Studio Code Intellisense doesn't show function documentation for C++

I followed this C/C++ for Visual Studio Code article from Microsoft to write C++ using Visual Studio Code. Unlike the article showing that intellisense provides documentation for member functions, it doesn't show me any. Same thing also happens in Visual Studio 2019 too. Here is a screenshot of how my intellisense looks.
What I tried to fix it so far:
Reinstall VSCode (deleted the app data as well)
Edit*
Configuration Files
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe",
"cStandard": "c17",
"intelliSenseMode": "windows-msvc-x64",
"cppStandard": "c++14"
}
],
"version": 4
}
settings.json
{
"files.associations": {
"xstring": "cpp"
}
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The documentation shown in the mouseover tooltip is generated from the standard library source files configured to be used. The screenshot from the VS Code docs is from a setup using gcc's c++ standard library implementation, libstdc++, which has c++ comments containing that documentation. Since you are using Visual Studio, you are using Microsoft's implementation of the c++ standard library. Browsing through their headers, it seems that the Microsoft STL doesn't include such documentation comments.
You could ask a question on their discussion board to ask if there are good workarounds/alternatives, or (politely) ask why they don't maintain such documentation comments, or if they have plans to in the future.
I haven't used this, but you may be interested to try this VS Code extension: Guyutongxue.cpp-reference, which
is a tool to browse cppreference.com from within vscode, instead of going to the browser to do so. You can use this extension to search for library and methods documentation of the C++ standard.

How Do I Set Include Path Library Directory And Linker For VSCODE When I Have Visual C++ Build Tools To Work With (not g++)

I have set up VS code as my development environment and used
MSVC build tools (cl.exe compiler) instead of g++. I tried to set up SFML for my environment. I want to know how do I set the SFML include path and library path. And also, How do I perform static Linking with cl.exe. Note: I am using only VS code and NOT Visual Studio for programming. Below are some files I've used. Tasks.json,Launch.json, C_cpp_properties.json.
Tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe:",
"${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
"${workspaceFolder}\\*.cpp"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "cl.exe - Build and debug active file",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal",
"preLaunchTask": "C/C++: cl.exe build active file"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${default}"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.30.30705/bin/Hostx64/x64/cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
As mentioned in the comments, you need to use a C++ build system to manage dependencies and build your project. VSCode does not come with any built-in build systems like Visual Studio does.
VSCode tasks allow you to specify a command line, which can then be invoked easily in the IDE. The task shown is just a "build active file" task, which is only really useful for trivial programs with no dependencies. It invokes cl.exe on the current source file (and passes a few other arguments).
You can specify include directories and pass arguments to the linker by adding to the "args" array in the task, e.g.:
"/I", "D:\\Code Libraries\\boost_1_77_0",
"/link", "/LIBPATH:\"D:\\Code Libraries\\boost_1_77_0\\stage\\lib\"",
which assumes that the boost headers and (statically built) libraries are at the specified locations.
You could probably work out how to build an entire project by adding command lines with VSCode tasks, but it's probably easier to use a build system (even if that system is CMake).

I have a build error with MinGW and VS Code "g++ not recognized as a cmdlet..."

I am trying to set up build and run a c++ file in VS Code 2019. I am having build errors after editing the tasks.json file. The environment variable is set to g++ as it should be. So far, I have been following this tutorial.
I attempted changing "command" to"C:\MinGW\bin\g++.exe" as recommended in a question thread on GitHub. However, because my c++ file is not in this file path, the program was not able to find it when I built the code. This is what the "command" portion of the tasks.json file should look like:
"label": "build calculator adventure",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"Calculator-Adventure",
"Calculator Adventure.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
The "Calculator-Adventure" part is my filename. The expected output is for the code to build and create a .exe file for my code, as stated in the tutorial and said in the VS Code Docs.
However, it currently outputs the following into the terminal:
> Executing task: ‪‪g++ -g Calculator Adventure.cpp -o Calculator-Adventure <
g++ : The term 'g++' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
The terminal process terminated with exit code: 1"
OK, I finally figured it out. What worked for me was adding the file path to the git bash shell (C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git) to the System Environment Variables in the Control Panel (how to do that here). Make sure you also have the file path to the MinGW bin folder added to the Environment Variables as well (32bit installer: C:\MinGW\bin) (64bit installer: C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin) Then, restart VS Code and build (Ctrl+Shift+B) again.
Here's my final code for the .json files:
c_cpp_properties.json:
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build calculator adventure",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"Calculator-Adventure",
"Calculator Adventure.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
For more information, check out this page. It is a really detailed step-by-step guide for using the MinGW compiler for C++ in VS Code (read it carefully). If you have any other trouble, take a look at this tutorial (same tutorial linked in the question). Hope this helps!
Note: in the docs page I linked, they use the 64bit version of MinGW. It should still work with the 32bit version though. Thanks, to #drescherjm for posting the VS Code Docs!

Better Pretty Printing of 2d arrays in VS Code Debugger - C++

Hi,
I'm pretty to new to C++ programming on VS Code, so I have a couple of Questions to ask.
My Software Specs:
Linux, 64 bit, Deepin 15.9
VS Code 1.31.1
G++ - 8, GDB 7.12
Extension used : C/C++ IntelliSense, debugging, and code browsing by Microsoft
C++ Pretty Printing of 2D array
I have to debug a lot of multi-dimensional arrays while programming and thus I want to know a method on how to view a 2D array, in the 'variables' window('watch' window will also do), in the form of a matrix.
Currently my variable's window looks like this
I want it to resemble something like
I know that a 2D array can be viewed using the method posted here, but I want to view it inside the debug tab in VSCode. Is there any pretty printing method or code to help me accomplish my goal?
Random Message On The Terminal When Stopping The Debug Process
There is some sort of message that appears on the terminal after stopping the debug process of C++. This is what it looks like -
I switched from opening an external terminal for debugging to using the inbuilt one, since, after I stop the debug process, the above message is displayed and the terminal doesn't auto-exit, so I would have to close the terminal manually. This was not the case on windows, where the terminal would auto-exit after the debugging. Is there any way to prevent this message(It comes after the Debug finishes or is stopped)?
I want to use the external terminal for debugging, so is there a method to remove this error message, or auto-exit after debugging closes?
These are my *.json files
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/g++-8",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"setupCommands": [
{
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"_runner": "terminal",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "g++-8",
"args": [
"-g",
"${file}"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Any help is truly Appreciated!!
I guess your options are writing your own gdb pretty printer or switching to lldb and the following extension which allows advanced visualization: https://github.com/vadimcn/vscode-lldb/wiki/Data-visualization

Visual Studio Code C++: unordered_map not found

I was given some C++ files that I need to compile. I'm using Visual Studio Code with the C/C++ and Code Runner extensions on Windows 10. With the following "include" statements:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <unordered_map>
I get the following error:
unordered_map: No such file or directory
I am very new to C++, and haven't been able to find a solution to this problem. I've updated the "includePath" in my c_cpp_properties.json file as follows. I have also tried compiling with Cygwin and Visual Studio Community, but I get the same error. I know the unordered_map .hpp file exists, but the compiler doesn't seem to be finding it.
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.15.26726/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17134.0",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
If it's relevant, this is what my tasks.json file looks like:
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build"
],
"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"
}
]
Are my .json files configured properly? I apologize if I'm missing something basic; I've done a lot of searching on how to compile C++ on Windows, and haven't had any success. Thank you in advance for any help.
EDIT:
Here is the full file I'm trying to compile. The executable is meant to be called by a python script.
https://github.com/jorpjomp/sierra-hotel/blob/master/location_routing.cpp
Unordered map is not supported in VS Code by default Microsoft ms-vscode.cpptools. Follow these steps to get over the problem:
Download MinGW from ( https://sourceforge.net/projects/mingw/ ). MinGW is a native Windows port of the GNU Compiler Collection (GCC), with freely distributable import libraries and header files for building native Windows applications.
Mark all the packages for installation.
ss to mark all the packages for installation
Click on the Apply Changes option under the Installation tab
ss of where to click on apply changes
Now, the Environment Variable’s Path is to be updated. Go to Advanced System Settings->Environment Variables.
Edit Path in System Variables Tab.
ss of how to edit Path
Copy the path of the bin folder of MinGW. By default, the path is: C:\MinGW\bin
Paste this new path in the list and click OK.
ss after pasting the bin path into the list
Run the C++ code in VS Code. It will work fine.
ss of vs code working fine at last
You are using msbuild at the moment to build your project. Is this intentional? If you just have "some C++ files" you want to compile, msbuild is an overkill, compile the source directly by either using Mingw's g++ or the Microsoft CL.exe compiler.
So I recommend:
1) Go to http://mingw-w64.org/doku.php/download, download and install mingw and add the path to g++ into your PATH environment variable.
2) In Visual Studio Code create a task.json with the following content:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json (assuming you store mingw here: C:\mingw\mingw64\bin):
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/mingw/mingw64/bin",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}