Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Configure VS Code For Microsoft C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Configure VS Code for Microsoft C++

In this tutorial, you configure Visual Studio Code to use the Microsoft C++ compiler
and debugger on Windows. After configuring VS Code, you will compile and debug a
simple program to get familiar with the VS Code user interface. After completing this
tutorial, you will be ready to create and configure your own workspace, and to explore
the VS Code documentation for further information about its many features. This
tutorial does not teach you details about Windows or the Microsoft C++ toolset or the
C++ language. For those subjects, there are many good resources available on the Web.

If you have any problems, feel free to file an issue for this tutorial in the VS Code
documentation repository.

Prerequisites
To successfully complete this tutorial, you must do the following:

1. Install Visual Studio Code.


2. Install the C++ extension for VS Code.
3. Install the Microsoft C++ (MSVC) compiler toolset.
o If you have a recent version of Visual Studio, open the Visual Studio
Installer from the Windows Start menu and verify that the C++ workload
is checked. If it's not installed, then check the box and click the Modify
button in the installer.
o Or, download the standalone toolset by clicking the Visual Studio Build
Tools link on the Downloads page and follow the prompts. With the
default settings, the download size is about 1GB and the size on disk is
about 4GB. If you don't require CMake support (and the Windows SDK
that it depends on) the size on disk should be about 3GB.

Start VS Code from the Developer Command Prompt


To use MSVC in VS Code, you must start VS Code from a Developer Command
Prompt for Visual Studio. An ordinary Windows command prompt, or a Bash prompt,
does not have the necessary environment variables set.

1. To open the Developer Command Prompt for VS, start typing "Developer" in
the Windows Start menu, and you should see it appear in the list of suggestions.
The exact name depends on which version of Visual Studio or the Visual Studio
Build Tools you have installed. Click on the item to open the prompt.
1. From the developer command prompt, create an empty folder called "projects"
where you can store all your VS Code projects, then create a subfolder called
"helloworld", navigate into it, and open VS Code (code) in that folder (.) by
entering the following commands:

mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .

The code . command opens VS Code in the current working folder, which becomes
your workspace. Before we can get IntelliSense support, or compile and debug our
code, we have to configure VS Code for MSVC. After completing the configuration, we
will have three files in a .vscode subfolder:

 c_cpp_properties.json (compiler path and IntelliSense settings)


 tasks.json (build instructions)
 launch.json (debugger settings)

Configure the compiler path


1. Press Ctrl+Shift+P to open the Command Palette. It looks like this:
2. Start typing "C/C++" and then choose Edit Configurations (UI) from the list of
suggestions. This opens the C/C++ Configurations page. When you make
changes here, VS Code writes them to a file called c_cpp_properties.json in
the .vscode folder.

3. Find the Compiler path setting. VS Code will attempt to populate it with a
default compiler based on what it finds on your system. It first looks for the
MSVC compiler, then for g++ on Windows Subsystem for Linux (WSL), then
for g++ on Mingw-w64. For MSVC, the path should look something like this,
depending on which specific version you have installed: "C:/Program Files
(x86)/Microsoft Visual
Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe"
.

The Compiler path setting is the most important setting in your configuration.
The extension uses it to infer the path to the C++ standard library header files.
When the extension knows where to find those files, it can provide lots of useful
information to you as you write code. This information is called IntelliSense and
you'll see some examples later in this tutorial.

4. Set IntelliSense mode to ${default}, which on Windows is msvc-x64.

You only need to modify the Include path setting if your program includes header files
that are not in your workspace or in the standard library path.

Visual Studio code places these settings in .vscode/c_cpp_properties.json. If you


open that file directly, it should look something like this (depending on your specific
path):

{
"configurations": [
{
"name": "Win32",
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"compilerPath": "C:/Program Files (x86)/Microsoft Visual
Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.ex
e",
"windowsSdkVersion": "10.0.17763.0",
"intelliSenseMode": "msvc-x64",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}

Create a build task


Next, create a tasks.json file to tell VS Code how to build (compile) the program.
This task will invoke the Microsoft C++ compiler (cl.exe) to create an executable file
based on the source code.

1. From the main menu, choose View > Command Palette and then type "task"
and choose Tasks: Configure Default Build Task. In the dropdown, select
Create tasks.json file from template, then choose Others. VS Code creates a
minimal tasks.json file and opens it in the editor.
2. Go ahead and replace the entire file contents with the following code snippet:

{
"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"
}
]
}

The label value will be used in the VS Code Command Palette and can be whatever
name you like. The command value says that we are using cl.exe, the MSVC compiler.
The args array specifies the command-line arguments that will be passed to the
compiler that was specified in the previous step. They must appear in the order expected
by the compiler. In this example, we are specifying the exception handling mode (EHsc)
and telling the compiler to produce a debug build with symbols (Zi). The /Fe:
argument tells the compiler to name the executable "helloworld.exe".

The group value specifies that this task will be run when you press Ctrl+Shift+B.

Configure debug settings


Next, you'll create a launch.json file to configure VS Code to launch the debugger
when you press F5 to debug the program.

1. From the main menu, choose Debug > Add Configuration... and then choose
C/C++ Windows (Launch). This causes the launch.json file to be created and
opened. Replace the contents of the file with this

{
"version": "0.2.0",
"configurations": [
{
"name": "(msvc) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/helloworld.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
}
]
}

Note that the program name helloworld.exe matches what we specified in


tasks.json.
By default, the C++ extension adds a breakpoint to the first line of main. The
stopAtEntry value is set to true to cause the debugger to stop on that breakpoint. You
can set this to false if you prefer to ignore it.

VS Code is now configured to use the Microsoft C++ compiler. The configuration
applies to the current workspace. To reuse the configuration, just copy the three JSON
files to a .vscode folder in a new workspace and change the names of the source file(s)
and executable as needed.

The remaining steps are provided as an optional exercise to help you get familiar with
the editing and debugging experience.

Add a source code file


1. In the main VS Code menu, click on File > New File and name it
helloworld.cpp.
2. Paste in this source code:
3. #include <iostream>
4. #include <vector>
5. #include <string>
6.
7. using namespace std;
8.
9. int main()
10. {
11.
12. vector<string> msg {"Hello", "C++", "World", "from", "VS
Code!"};
13.
14. for (const string& word : msg)
15. {
16. cout << word << " ";
17. }
18. cout << endl;
}

19. Now press Ctrl+S to save the file. Now notice how all the files we have just
edited appear in the File Explorer view in the left panel of VS Code:
This same panel is also used for source control, debugging, searching and replacing text,
and managing extensions. The buttons on the left control those views. We'll look at the
Debug View later in this tutorial. You can find out more about the other views in the
VS Code documentation.

Explore IntelliSense
In your new helloworld.cpp file, hover over vector or string to see type
information. After the declaration of the msg variable, start typing msg. as you would
when calling a member function. You should immediately see a completion list that
shows all the member functions, and a window that shows the type information for the
msg object:

You can press the TAB key to insert the selected member; then, when you add the
opening parenthesis, you will see information about any arguments that the function
requires.

Build the program


1. To run the build task that you defined in tasks.json, press Ctrl+Shift+B or
from the main menu choose View > Command Palette and start typing "Tasks:
Run Build Task". The option will appear before you finish typing.

Note: If you see an error message that looks like this: cl.exe: command not
found, it means you have not started VS Code from a Developer Command
Prompt for VS. See the first section of this tutorial for more information.

2. When the task starts, you should see the integrated Terminal window appear
below the code editor. After the task completes, the terminal shows output from
the compiler that indicates whether the build succeeded or failed. For a
successful MSVC build, the output looks something like this:

1. As the message instructs, press any key to close the build message; the terminal
now returns to the shell command prompt.

Start a debugging session


1. You are now ready to run the program. Press F5 or from the main menu choose
Debug > Start Debugging. Before we start stepping through the code, let's take
a moment to notice several changes in the user interface:

 The code editor highlights the first line in the main method. This is a breakpoint
that the C++ extension automatically sets for you:
 The workspace pane on the left now shows debugging information, but there
isn't any useful information yet because no program statements have been
executed yet, other than some system startup code, which is reflected in the
Threads window:
 At the top of the code editor, a debugging control panel appears. You can move
this around the screen by grabbing the dots on the left side.

Step through the code


Now we're ready to start stepping through the code.

1. Click or press the Step over icon in the debugging control panel until the for
(const string& word : msg) statement is highlighted.
The Step Over command skip over all the internal function calls within the
vector and string classes that are invoked when the msg variable is created
and initialized. Notice the change in the Variables window on the left. In this
case, the errors are expected because, although the variable names for the loop
are now visible to the debugger, the statement has not executed yet, so there is
nothing to read at this point. The contents of msg are visible, however, because
that statement has completed.

2. Press Step over again to advance to the next statement in this program (skipping
over all the internal code that is executed to initialize the loop). Now, the
Variables window shows information about the loop variables.
3. Press Step over again to execute the cout statement. Note As of the March
2019 version of the extension, no output is displayed until the loop completes.
4. If you like, you can keep pressing Step over until all the words in the vector
have been printed to the console. But if you are curious, try pressing the Step
Into button to step through source code in the C++ standard library!

To return to your own code, one way is to keep pressing Step over. Another
way is to set a breakpoint in your code by switching to the helloworld.cpp tab
in the code editor, putting the insertion point somewhere on the cout statement
inside the loop, and pressing F9. A red dot appears in the gutter on the left to
indicate that a breakpoint has been set on this line.
Then press F5 to start execution from the current line in the standard library
header. Execution will break on cout. If you like, you can press F9 again to
toggle off the breakpoint.

Set a watch
Sometimes you might want to keep track of the value of a variable as your program
executes. You can do this by setting a watch on the variable.

1. Place the insertion point inside the loop. In the Watch window, click the plus
sign and in the text box, type word, which is the name of the loop variable. Now
view the Watch window as you step through the loop.

2. Add another watch by adding this statement before the loop: int i = 0;. Then,
inside the loop, add this statement: ++i;. Now add a watch for i as you did in
the previous step.
3. To quickly view the value of any variable while execution is paused on a
breakpoint, you can simply hover over it with the mouse pointer.

Next steps
 Explore the VS Code User Guide.
 Review the Overview of the C++ extension.
 Create a new workspace, copy your .json files to it, adjust the necessary settings
for the new workspace path, program name, and so on, and start coding!

You might also like