Masoud Rahimi
Posted on September 15, 2020
Using VSCode as your main development IDE is quite interesting as it offers many features with the support of most programming languages.
To develop C/C++ applications you need to have a separate compiler installed, you can either use MinGW or Microsoft Visual C++ compiler. In this tutorial, I'm giving you a brief tutorial about using VSCode to develop C/C++ applications with MSVC on Windows.
There is an official tutorial about setting the environment but it may be unclear in some parts (e.g. it force you to use Development Command Prompt each time you open up VSCode). So I'm adding an alternative for those parts and create a custom build configuration.
Prerequisites
- Download and install VSCode from the official website.
- Install Microsoft Visual C++ toolset. You can use Visual Studio Installer and select
C++ build tools
. To verify the installation open up Developer Command Prompt from the Start menu and check thecl.exe
command. - Install C/C++ extension for VSCode from here.
Setup instructions
Create a directory for your project and put the required files in it. Then open the directory with VSCode. You can either use the command
code .
in terminal or right-click inside the folder and chooseopen with Code
.-
Create a simple hello world C++ file called
main.cpp
in the root of the project.
#include <iostream> using namespace std; int main() { cout << "It Works!" << endl; cin.get(); }
Press F1 or
Ctrl+Shift+P
to open Command Palette and chooseC/C++ Edit Configurations UI
. This will open up the configuration windows. Choose theWin32
for configuration name and set a proper compiler path. It should automatically set the path forcl.exe
e.gC:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.21.27702/bin/Hostx64/x64/cl.exe
. You can set other options likeIntelliSense mode
,Include path
and etc.-
Create an empty
settings.json
file in.vscode
directory and add the two parameters forcl.exe
path and build directory path (if the file already exists just add these two values). In this configuration, we are creating a build directory calledbuild
to store the binaries. Also, we create a custom build command calledbuildCommand
to executecl.exe
from the VS Command Prompt.
{ "buildDir": "${workspaceRoot}\\build", "buildCommand": "C:\\Program^ Files^ ^(x86^)\\Microsoft^ Visual^ Studio\\2019\\Enterprise\\Common7\\Tools\\VsDevCmd.bat && cl" }
We would be able to use these variables in other config files with
${config:<ConfigName>}
syntax. -
Next, We are going to create a
tasks.json
file and create a task for our build. Create thetasks.json
inside.vscode
directory and fill it like below:
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "Set dependencies for build", "command": "if not exist ${config:buildDir} mkdir ${config:buildDir}" }, { "type": "shell", "label": "C/C++: cl.exe build active file", "command": "${config:buildCommand}", "args": [ "/Zi", "/EHsc", "/Fe:", "${config:buildDir}\\${fileBasenameNoExtension}.exe", "/Fo${config:buildDir}\\", "/Fd${config:buildDir}\\", "${file}" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$msCompile" ], "group": { "kind": "build", "isDefault": true }, "dependsOn": [ "Set dependencies for build" ] } ] }
The arguments for
cl.exe
are quite standard but if you want to add more options you can add them in theargs
section. To verify the installation run the build task with
Ctrl+Shift+B
. This will create thebuild
directory and put the output.-
To debug your application create a
launch.json
file in.vscode
a directory like below:
{ "version": "0.2.0", "configurations": [ { "name": "cl.exe - Build and debug active file", "type": "cppvsdbg", "request": "launch", "program": "${config:buildDir}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${env:buildDir}", "environment": [], "externalConsole": false, "preLaunchTask": "C/C++: cl.exe build active file" } ] }
This is a default configuration and I already change the
program
path. I also add apreLaunchTask
to rebuild the project before launching the debug session. After creating the debug configuration you can put your breakpoints and launch the debug session with F5.
Resources
All resource files can be accessed from my GitHub repository.
Posted on September 15, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.