How to compile c visual studio code

How to compile c visual studio code

How to compile your C++ code in Visual Studio Code

PS: This was published on my Blog here.

C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.

In simple terms, C++ is a sophisticated, efficient, general-purpose programming language based on C.

It was developed by Bjarne Stroustrup in 1979.

One of C++’s main features is the compiler. This is used to compile and run C++ code.

A compiler is a special program that processes statements written in a particular programming language like C++ and turns them into machine language or «code» that a computer’s processor uses. (Source)

I actually wrote this article because I had a C++ assignment which required using a compiler. As usual, everyone was using the CodeBlocks IDE and Visual Studio IDE. But I was already used to Visual Studio Code for all my programming stuff.

I then set out to find a way of compiling C++ directly inside my own VsCode Editor, hence this article :).

In this article, I’ll show you how to set up your compiler in VsCode and give you some links to some of the best C++ resources.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Prerequisites

Disclaimer!

I will be using a Windows OS throughout this article, but I’ll provide links to resources that will help those using other operating systems.

Now let’s get started!

Download and install a C++ compiler

MinGW, a contraction of «Minimalist GNU for Windows», is a minimalist development environment for native Microsoft Windows applications. (Source)

Edit your PATH environment variable to include the directory where the C++ compiler is located

Install Code Runner extension in VS Code

Now we have our compiler set up, let’s install Code Runner

Code Runner allows you to Run code snippet or code file for multiple languages:

Save this file as test.cpp

Run your code using Code Runner

The code will run and the output will be shown in the Output Window. Open the output window with `Ctrl+ shortcut.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

To stop the running code

Hurray, you just successfully set up your C++ environment in VsCode!

Conclusion

Here’s a quick hint: By default, VsCode’s output terminal is read-only. If you’re running code that requires user input like:

Hurray, you’re done and ready to roll :).

C++ resources

Here are some C++ resources you can use to get started with learning C++

How do I set up Visual Studio Code to compile C++ code?

Microsoft’s Visual Studio Code editor is quite nice, but it has no default support for building C++ projects.

How do I configure it to do this?

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

14 Answers 14

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

There is a much easier way to compile and run C++ code, no configuration needed:

Moreover you could update the config in settings.json using different C++ compilers as you want, the default config for C++ is as below:

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

The build tasks are project specific. To create a new project, open a directory in Visual Studio Code.

The tasks.json file will be opened. Paste the following build script into the file, and save it:

Now go to menu FilePreferencesKeyboard Shortcuts, and add the following key binding for the build task:

Now when you press F8 the Makefile will be executed, and errors will be underlined in the editor.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

A makefile task example for new 2.0.0 tasks.json version.

In the snippet below some comments I hope they will be useful.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Here is how I configured my VS for C++

Make sure to change appropriete paths to where your MinGW installed

launch.json

tasks.json

c_cpp_properties.json

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

tasks.json

description of type property :

focus: Controls whether the terminal is taking input focus or not. Default is false.

echo: Controls whether the executed command is echoed in the terminal. Default is true.

showReuseMessage: Controls whether to show the «Terminal will be reused by tasks, press any key to close it» message.

clear: Controls whether the terminal is cleared before this task is run. Default is false.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Out of frustration at the lack of clear documentation, I’ve created a Mac project on github that should just work (both building and debugging):

Note that it requires XCode and the VSCode Microsoft cpptools extension.

I plan to do the same for Windows and linux (unless Microsoft write decent documentation first. ).

First of all, goto extensions (Ctrl + Shift + X) and install 2 extensions:

Then, then reload the VS Code and select a play button on the top of the right corner your program runs in the output terminal. You can see output by Ctrl + Alt + N. To change other features goto user setting. How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

The basic problem here is that building and linking a C++ program depends heavily on the build system in use. You will need to support the following distinct tasks, using some combination of plugins and custom code:

General C++ language support for the editor. This is usually done using ms-vscode.cpptools, which most people expect to also handle a lot of other stuff, like build support. Let me save you some time: it doesn’t. However, you will probably want it anyway.

Build, clean, and rebuild tasks. This is where your choice of build system becomes a huge deal. You will find plugins for things like CMake and Autoconf (god help you), but if you’re using something like Meson and Ninja, you are going to have to write some helper scripts, and configure a custom «tasks.json» file to handle these. Microsoft has totally changed everything about that file over the last few versions, right down to what it is supposed to be called and the places (yes, placeS) it can go, to say nothing of completely changing the format. Worse, they’ve SORT OF kept backward compatibility, to be sure to use the «version» key to specify which variant you want. See details here:

. but note conflicts with:

WARNING: IN ALL OF THE ANSWERS BELOW, ANYTHING THAT BEGINS WITH A «VERSION» TAG BELOW 2.0.0 IS OBSOLETE.

Configure VS Code for Microsoft C++

In this tutorial, you configure Visual Studio Code to use the Microsoft Visual C++ compiler and debugger on Windows.

After configuring VS Code, you will compile and debug a simple Hello World program in VS Code. This tutorial does not teach you details about 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:

Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for ‘c++’ in the Extensions view ( ⇧⌘X (Windows, Linux Ctrl+Shift+X ) ).

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Install the Microsoft Visual C++ (MSVC) compiler toolset.

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 select the Modify button in the installer.

You can also install the Desktop development with C++ workload without a full Visual Studio IDE installation. From the Visual Studio Downloads page, scroll down until you see Tools for Visual Studio 2022 under the All Downloads section and select the download for Build Tools for Visual Studio 2022.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

This will launch the Visual Studio Installer, which will bring up a dialog showing the available Visual Studio Build Tools workloads. Check the Desktop development with C++ workload and select Install.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Note: You can use the C++ toolset from Visual Studio Build Tools along with Visual Studio Code to compile, build, and verify any C++ codebase as long as you also have a valid Visual Studio license (either Community, Pro, or Enterprise) that you are actively using to develop that C++ codebase.

Check your Microsoft Visual C++ installation

To use MSVC from a command line or VS Code, you must run from a Developer Command Prompt for Visual Studio. An ordinary shell such as PowerShell, Bash, or the Windows command prompt does not have the necessary path environment variables set.

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. Select the item to open the prompt.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

If the Developer Command Prompt is using the BuildTools location as the starting directory (you wouldn’t want to put projects there), navigate to your user folder ( C:\users\\ ) before you start creating new projects.

Note: If for some reason you can’t run VS Code from a Developer Command Prompt, you can find a workaround for building C++ projects with VS Code in Run VS Code outside a Developer Command Prompt.

Create Hello World

Add a source code file

Add hello world source code

Now paste in this source code:

Now press ⌘S (Windows, Linux Ctrl+S ) to save the file. Notice how the file you just added appears in the File Explorer view ( ⇧⌘E (Windows, Linux Ctrl+Shift+E ) ) in the side bar of VS Code:

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

You can also enable Auto Save to automatically save your file changes, by checking Auto Save in the main File menu.

The Activity Bar on the far left lets you open different views such as Search, Source Control, and Run. You’ll look at the Run view later in this tutorial. You can find out more about the other views in the VS Code User Interface documentation.

Note: When you save or open a C++ file, you may see a notification from the C/C++ extension about the availability of an Insiders version, which lets you test new features and fixes. You can ignore this notification by selecting the X (Clear Notification).

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:

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

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.

Run helloworld.cpp

Remember, the C++ extension uses the C++ compiler you have installed on your machine to build your program. Make sure you have a C++ compiler installed before attempting to run and debug helloworld.cpp in VS Code.

Open helloworld.cpp so that it is the active file.

Press the play button in the top right corner of the editor.

Choose C/C++: cl.exe build and debug active file from the list of detected compilers on your system.

After the build succeeds, your program’s output will appear in the integrated Terminal.

Your new tasks.json file should look similar to the JSON below:

Note: You can learn more about tasks.json variables in the variables reference.

The command setting specifies the program to run; in this case that is «cl.exe». The args array specifies the command-line arguments that will be passed to cl.exe. These arguments must be specified in the order expected by the compiler.

The label value is what you will see in the tasks list; you can name this whatever you like.

The detail value is what you will as the description of the task in the tasks list. It’s highly recommended to rename this value to differentiate it from similar tasks.

Modifying tasks.json

Debug helloworld.cpp

Explore the debugger

Before you start stepping through the code, let’s take a moment to notice several changes in the user interface:

The Integrated Terminal appears at the bottom of the source code editor. In the Debug Output tab, you see output that indicates the debugger is up and running.

The editor highlights the line where you set a breakpoint before starting the debugger:

The Run and Debug view on the left shows debugging information. You’ll see an example later in the tutorial.

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.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Step through the code

Now you’re ready to start stepping through the code.

Click or press the Step over icon in the debugging control panel.

This will advance program execution to the first line of the for loop, and 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.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

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.

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.

Press Step over again to execute the cout statement. (Note that as of the March 2019 release, the C++ extension does not print any output to the Debug Console until the loop exits.)

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!

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

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.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

To quickly view the value of any variable while execution is paused on a breakpoint, you can hover over it with the mouse pointer.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Customize debugging with launch.json

There are cases where you’d want to customize your debug configuration, such as specifying arguments to pass to the program at runtime. You can define custom debug configurations in a launch.json file.

You’ll then see a dropdown for various predefined debugging configurations. Choose C/C++: cl.exe build and debug active file.

VS Code creates a launch.json file, which looks something like this:

Change the stopAtEntry value to true to cause the debugger to stop on the main method when you start debugging.

From now on, the play button and F5 will read from your launch.json file when launching your program for debugging.

C/C++ configurations

If you want more control over the C/C++ extension, you can create a c_cpp_properties.json file, which will allow you to change settings such as the path to the compiler, include paths, C++ standard (default is C++17), and more.

You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ).

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

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

Compiler path

The compilerPath setting is an 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 useful features like smart completions and Go to Definition navigation.

The C/C++ extension attempts to populate compilerPath with the default compiler location based on what it finds on your system. The extension looks in several common compiler locations.

The compilerPath search order is:

If you have g++ or WSL installed, you might need to change compilerPath to match the preferred compiler for your project. For Microsoft C++, 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».

Reusing your C++ configuration

Run VS Code outside the Developer Command Prompt

In certain circumstances, it isn’t possible to run VS Code from Developer Command Prompt for Visual Studio (for example, in Remote Development through SSH scenarios). In that case, you can automate initialization of Developer Command Prompt for Visual Studio during the build using the following tasks.json configuration:

Troubleshooting

The term ‘cl.exe’ is not recognized

If you see the error «The term ‘cl.exe’ is not recognized as the name of a cmdlet, function, script file, or operable program.», this usually means you are running VS Code outside of a Developer Command Prompt for Visual Studio and VS Code doesn’t know the path to the cl.exe compiler.

VS Code must either be started from the Developer Command Prompt for Visual Studio, or the task must be configured to run outside a Developer Command Prompt.

How to compile and run a c++ source file in visual studio code

I’ve searched for an answer to this question, but couldn’t seem to find one. I know that we can use task.json files to automate the build process. But I want to use Visual Studio Code to implement algorithms in C++ for competitive programming. I want to be able to compile a program, and run it all in one go, if there aren’t any errors. If there are errors, I would like them to be displayed.

Also, visual studio code comes with an integrated terminal, so it would be nice if the program output could be redirected there. Also, how can we map a keyboard shortcut to run this task.

I’m using Visual Studio Code 2019 on Windows 10 with the MinGW G++ compiler.

EDIT

I’ve tried Escape0707’s answer below, and I tried executing ‘Run Code’ with the default key binding of Ctrl + Alt + N but I’m getting this error.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

2 Answers 2

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

Updated method which combines make and vscode-cpptools debug:

If you don’t care about VSCode integrated debugging tools, which will give you the ability to set breakpoints, change variable value during runtime, inspect variable value, and etc, and you want a somewhat easier, simpler, faster, transparent way to invoke the good old command line tools, skip this section and checkout Code Runner below.

The default configurations come with VSCode C++ extension are kind of slow for low-end machines. The worst part is that they will always rebuild your executable, and don’t support ‘Start Without Debugging’. Below is a workaround for Linux (and of course remote-WSL).

Use Intellisense to learn about each properties in configs.

tasks.json

Then, create the following launch.json :

launch.json

Original answer

Happy coding!

Update about make

Attention: must use Tab to indent the second line, not Space s.

How to compile c visual studio code

Configure VS Code for Microsoft C++

In this tutorial, you configure Visual Studio Code to use the Microsoft Visual C++ compiler and debugger on Windows.

After configuring VS Code, you will compile and debug a simple Hello World program in VS Code. This tutorial does not teach you details about 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.

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

Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for ‘c++’ in the Extensions view ( kb(workbench.view.extensions) ).

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Install the Microsoft Visual C++ (MSVC) compiler toolset.

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 select the Modify button in the installer.

You can also install the Desktop development with C++ workload without a full Visual Studio IDE installation. From the Visual Studio Downloads page, scroll down until you see Tools for Visual Studio 2022 under the All Downloads section and select the download for Build Tools for Visual Studio 2022.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

This will launch the Visual Studio Installer, which will bring up a dialog showing the available Visual Studio Build Tools workloads. Check the Desktop development with C++ workload and select Install.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Note : You can use the C++ toolset from Visual Studio Build Tools along with Visual Studio Code to compile, build, and verify any C++ codebase as long as you also have a valid Visual Studio license (either Community, Pro, or Enterprise) that you are actively using to develop that C++ codebase.

Check your Microsoft Visual C++ installation

To use MSVC from a command line or VS Code, you must run from a Developer Command Prompt for Visual Studio. An ordinary shell such as PowerShell, Bash, or the Windows command prompt does not have the necessary path environment variables set.

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. Select the item to open the prompt.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

If the Developer Command Prompt is using the BuildTools location as the starting directory (you wouldn’t want to put projects there), navigate to your user folder ( C:\users\\ ) before you start creating new projects.

Note : If for some reason you can’t run VS Code from a Developer Command Prompt, you can find a workaround for building C++ projects with VS Code in Run VS Code outside a Developer Command Prompt.

Create Hello World

Add a source code file

Add hello world source code

Now paste in this source code:

Now press kb(workbench.action.files.save) to save the file. Notice how the file you just added appears in the File Explorer view ( kb(workbench.view.explorer) ) in the side bar of VS Code:

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

You can also enable Auto Save to automatically save your file changes, by checking Auto Save in the main File menu.

The Activity Bar on the far left lets you open different views such as Search, Source Control, and Run. You’ll look at the Run view later in this tutorial. You can find out more about the other views in the VS Code User Interface documentation.

Note : When you save or open a C++ file, you may see a notification from the C/C++ extension about the availability of an Insiders version, which lets you test new features and fixes. You can ignore this notification by selecting the X (Clear Notification).

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:

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

You can press the kbstyle(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.

Remember, the C++ extension uses the C++ compiler you have installed on your machine to build your program. Make sure you have a C++ compiler installed before attempting to run and debug helloworld.cpp in VS Code.

Open helloworld.cpp so that it is the active file.

Press the play button in the top right corner of the editor.

Choose C/C++: cl.exe build and debug active file from the list of detected compilers on your system.

After the build succeeds, your program’s output will appear in the integrated Terminal.

Your new tasks.json file should look similar to the JSON below:

Note : You can learn more about tasks.json variables in the variables reference.

The command setting specifies the program to run; in this case that is «cl.exe». The args array specifies the command-line arguments that will be passed to cl.exe. These arguments must be specified in the order expected by the compiler.

The label value is what you will see in the tasks list; you can name this whatever you like.

The detail value is what you will as the description of the task in the tasks list. It’s highly recommended to rename this value to differentiate it from similar tasks.

Explore the debugger

Before you start stepping through the code, let’s take a moment to notice several changes in the user interface:

The Integrated Terminal appears at the bottom of the source code editor. In the Debug Output tab, you see output that indicates the debugger is up and running.

The editor highlights the line where you set a breakpoint before starting the debugger:

The Run and Debug view on the left shows debugging information. You’ll see an example later in the tutorial.

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.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Step through the code

Now you’re ready to start stepping through the code.

Click or press the Step over icon in the debugging control panel.

This will advance program execution to the first line of the for loop, and 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.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

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.

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.

Press Step over again to execute the cout statement. (Note that as of the March 2019 release, the C++ extension does not print any output to the Debug Console until the loop exits.)

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!

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

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.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

To quickly view the value of any variable while execution is paused on a breakpoint, you can hover over it with the mouse pointer.

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

Customize debugging with launch.json

There are cases where you’d want to customize your debug configuration, such as specifying arguments to pass to the program at runtime. You can define custom debug configurations in a launch.json file.

You’ll then see a dropdown for various predefined debugging configurations. Choose C/C++: cl.exe build and debug active file.

VS Code creates a launch.json file, which looks something like this:

Change the stopAtEntry value to true to cause the debugger to stop on the main method when you start debugging.

From now on, the play button and kb(workbench.action.debug.start) will read from your launch.json file when launching your program for debugging.

If you want more control over the C/C++ extension, you can create a c_cpp_properties.json file, which will allow you to change settings such as the path to the compiler, include paths, C++ standard (default is C++17), and more.

You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette ( kb(workbench.action.showCommands) ).

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

How to compile c visual studio code. Смотреть фото How to compile c visual studio code. Смотреть картинку How to compile c visual studio code. Картинка про How to compile c visual studio code. Фото How to compile c visual studio code

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

The compilerPath setting is an 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 useful features like smart completions and Go to Definition navigation.

The C/C++ extension attempts to populate compilerPath with the default compiler location based on what it finds on your system. The extension looks in several common compiler locations.

The compilerPath search order is:

If you have g++ or WSL installed, you might need to change compilerPath to match the preferred compiler for your project. For Microsoft C++, 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».

Reusing your C++ configuration

Run VS Code outside the Developer Command Prompt

In certain circumstances, it isn’t possible to run VS Code from Developer Command Prompt for Visual Studio (for example, in Remote Development through SSH scenarios). In that case, you can automate initialization of Developer Command Prompt for Visual Studio during the build using the following tasks.json configuration:

The term ‘cl.exe’ is not recognized

If you see the error «The term ‘cl.exe’ is not recognized as the name of a cmdlet, function, script file, or operable program.», this usually means you are running VS Code outside of a Developer Command Prompt for Visual Studio and VS Code doesn’t know the path to the cl.exe compiler.

VS Code must either be started from the Developer Command Prompt for Visual Studio, or the task must be configured to run outside a Developer Command Prompt.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *