I'm trying to compile C/C++ code in VS Code using cl (installed via visual studio 2019). I've set up the json files like the MS website suggests,
https://code.visualstudio.com/docs/cpp/config-msvc,
but I still get the error:
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.
Here are my json files:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.21.27702/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
{
"version": "2.0.0",
"tasks": [
{
"label": "msvc build",
"type": "shell",
"command": "cl.exe",
"args": [
"/EHsc",
"/Zi",
"/Fe:",
"helloworld.exe",
"test.c"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal":"always"
},
"problemMatcher": "$msCompile"
}
]
}
VS Code is first and foremost an editor, and relies on command-line tools to do much of the development workflow. The C/C++ extension does not include a C++ compiler or debugger. You will need to install these tools or use those already installed on your computer.
Open your C code file in Text Editor, then use shortcut Ctrl+Alt+N , or press F1 and then select/type Run Code , or right click the Text Editor and then click Run Code in context menu, the code will be compiled and run, and the output will be shown in the Output Window.
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.
Your "msvc build"
task only specifies "cl.exe"
for its command, with no leading path. The problem is that cl.exe
isn't on your PATH, or anywhere that VS Code can see when it goes to run your build task.
One solution to this is to open VS Code using the "Developer Command Prompt" for whatever Visual Studio version you have. This version of the command prompt defines the location of the Visual Studio build tools so that any program or command that is run from that cmd prompt will be able to find the programs like "cl.exe".
There is another solution that I prefer to use, which is to write a batch script for your build task.
Put this script in the root of your VSCode workspace and call it build.bat
:
:: set the path to your visual studio vcvars script, it is different for every version of Visual Studio.
set VS2017TOOLS="C:\Program Files(x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
:: make sure we found them
if not exist %VS2017TOOLS% (
echo VS 2017 Build Tools are missing!
exit
)
:: call that script, which essentially sets up the VS Developer Command Prompt
call %VS2017TOOLS%
:: run the compiler with your arguments
cl.exe /EHsc /Zi /Fe: helloworld.exe test.c
exit
Then your task has to be changed to run the batch script:
{
"label": "msvc build",
"type": "shell",
"command": "${workspaceFolder}/build.bat",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal":"always"
},
"problemMatcher": "$msCompile"
}
The advantage to using the batch script this way is that you do not need to run VS Code from the Developer Command Prompt, and the $msCompile
problem matcher will still be able to display errors and warnings inside VS Code.
Another note is that the batch script in this answer is specific to just your project. You can continue to update that script to build your project as it gets larger, or you could take advantage of a tool like CMake to handle the generation of the actual build scripts from a configuration file.
Then your build.bat
may look more like this:
:: set the path to your visual studio vcvars script, it is different for every version of Visual Studio.
set VS2017TOOLS="C:\Program Files(x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
:: make sure we found them
if not exist %VS2017TOOLS% (
echo VS 2017 Build Tools are missing!
exit
)
:: call that script, which essentially sets up the VS Developer Command Prompt
call %VS2017TOOLS%
:: Set some variables for the source directory and the build directory
set SrcDir=%CD%
set BuildDir=%CD%\build
:: Make the build directory if it doesn't exist
if not exist "%BuildDir%" mkdir "%BuildDir%"
:: Make sure you configure with CMake from the build directory
cd "%BuildDir%"
:: Call CMake to configure the build (generates the build scripts)
cmake %SrcDir%^
-D CMAKE_C_COMPILER=cl.exe^
-D CMAKE_CXX_COMPILER=cl.exe^
:: Call CMake again to build the project
cmake --build %BuildDir%
exit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With