There is no benefit in reinstalling VS Code. There is nothing wrong with the installation. Ok, let's start at the beginning and note that I'm basically following the steps given here.
Assumptions:
- You already have a folder with your source files in them.
- You are labeling your files
.c
to clarify this is C files and not C++. This has a minor impact on the build files later. - You have already installed Visual Studio 2019 or the Visual Studio 2019 Build Tools so you have access to the C++ debugger and compilers.
Now the steps.
- You must start VS Code from the Developer Command Prompt that is generated when you install VS. This is how VS Code will be able to locate the MSVC components it needs. For now go that route. Later you can probably find the documentation that discusses how to configure the paths and run the same commands as the command prompt (it is just a shell script) so you can forgo this extra step. But let's just get it working for now.
- In VS Code open the folder containing your project using
File\Open Folder
. This will ensure all files are stored in the correct spot. - Invoke the Command Palette by right clicking inside the source file and selecting that option. Then type
Configuration
and choose the option to edit the C/C++ configuration. Note that this is editing the C/C++ settings for only this folder, not globally. - It should auto-generate the
c_cpp_properties.json
file. This is where you would change your language option fromc17
toc11
or whatever you want to use. {
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${vcpkgRoot}/x86-windows/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "<<pathtoVS2019>/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
} - Go back to the command palette and select the command to edit the
launch.json
file. Then chooseC++ Windows
and finally the option forcl.exe
. It should auto generate thelaunch.json
andtasks.json
files for you. -
launch.json
is the settings used to launch the debugger. Thetasks.json
file is where tasks are stored including building. - In the
launch.json
file it is configured to debug an exe that matches the current file name. Since your project probably has multiple source files you should change it to the name of the program instead. Note also that it includes a pretask to build the code first. The prelaunch task name must exactly match what is intasks.json
. - Inside
tasks.json
is the compiler build commands. It by default only builds the current source file so you'll want to change that to build all files. Also ensure that the generated output file matches what the debugger wants to run. - Once all the files are in place then simply pressing F5 should trigger a build and then start the debugger. Output should go in the integrated terminal but you can change that if you want.
launch.json (in .vscode
folder)
{
"version": "0.2.0",
"configurations": [
{
"name": "cl.exe - Build and debug active file",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "integratedTerminal",
"preLaunchTask": "C/C++: cl.exe build active file"
}
]
}
tasks.json (in .vscode
folder)
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe:",
"${fileDirname}\\test.exe",
"${fileDirname}\\*.c"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}