Visual Studio Code MSVC cl.exe not found after installing build tools - c++

After setting up VS Code, installing the build tools and going through the tutorial here: https://code.visualstudio.com/docs/cpp/config-msvc
Visual Studio Code is unable to find the cl.exe to compile C++.
I replaced the path from the tutorial with the correct one on my hard drive (cl.exe is there).
// My Config
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.22.27905/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
// The tutorial build-task
{
"version": "2.0.0",
"tasks": [
{
"label": "msvc build",
"type": "shell",
"command": "cl.exe",
"args": [
"/EHsc",
"/Zi",
"/Fe:",
"helloworld.exe",
"helloworld.cpp"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal":"always"
},
"problemMatcher": "$msCompile"
}
]
}
When running the build task this error shows, although the compilerPath is correct (the cl.exe is there) and helloworld.cpp exists as well. Running everything as administrator didn't help.
cl.exe : The term 'cl.exe' 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.
At line:1 char:1
+ cl.exe /EHsc /Zi /Fe: helloworld.exe helloworld.cpp
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (cl.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

The 'c_cpp_properties.json' file only configures IntelliSense in the C/C++ extension, so the compilerPath option does not help with building.
Make sure you are launching VS Code from the Developer Command Prompt. This will set the necessary environment variables, including the location of 'cl.exe'.

In the documentation of Visual Studio Code you can see a solution to this issue.
In the section of "C++ > Microsoft C++ on Windows > Troubleshooting" they explain that you need to open your projects from the Developer Command Promp
As an example:
cd projects/yourproject
code .
I haven't found other way of doing it.

My first answer had used hardcoded paths, but this is better. You only need to pay attention to the paths since the docs use 2019 etc.
The VsDevCmd.bat sets up all the env vars you'll need and it works easily within VSCode. It also gives you compiler errors which my previous solution with the hardcoded cl.exe paths did not for some reason.
Run VS Code outside the Developer Command Prompt

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.

Can't manage to set up environment and path to libraries under Visual studio for c++

After having installed the MongoDB drivers for c++ following this tutorial, I wrote a little bit of code that I managed to compile and run with the command:
c++ --std=c++11 main.cpp -o app $(pkg-config --cflags --libs libmongocxx) && D_LIBRARY_PATH=/usr/local/lib ./app
Now, I try to debug and launch my code with Visual code. I don't use Neither Visual studio because I'm under Linux nor NetBeans because I don't have the proper JDK nor Code::Blocks because it is too complicated to set up with the proper environment.
My problem is that Visual Code can't manage to find the several #include I need to work with MongoDB and Bsoncxx:
I tried setting up a launch.json file with the parameter
"environment": [
{
"name": "LD_LIBRARY_PATH",
"value": "/usr/local/lib"
}
]
But I'm sure I'm doing totally wrong and I can't manage to find any solution over the internet. I could continue to write code then compile and run it via Batch files but an IDE is way more convenient.
To get Intellisense working you'll need to tell Vscode where your includes are. To do this, do the following:
Install the C/C++ extension for VSCode
Enter your include paths to in c_cpp_properties.json
Here is an example of the c_cpp_properties.json file I've used for a small project on WSL.
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/deps/fmt/include/",
"${workspaceFolder}/deps/spdlog/include/",
"${workspaceFolder}/deps/CLI11/include/",
"${workspaceFolder}/deps/pugixml/include/"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Follow this link for a full tutorial on how to set this up. How to set up VSCode
Pay special attention to the Configure the compiler path section and the Create a build task sections

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.

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!

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
}