How Do I Set Up Visual Studio Code To Compile C++ Code?

One of the key features of Visual Studio is its great debugging support. VS"s built-in debugger helps accelerate your edit, compile, & debug loop.

Bạn đang xem: How do i set up visual studio code to compile c++ code?


Debugger extensions

VS has built-in debugging tư vấn for the Node.js runtime and can debug JavaScript, TypeScript, or any other language that gets transpiled lớn JavaScript.

For debugging other languages và runtimes (including PHP, Ruby, Go, C#, Python, C++, PowerShell and many others), look for Debuggers extensions in the VS Marketplace or select Install Additional Debuggers in the top-level Run menu.

Below are several popular extensions which include debugging support:

Tip: The extensions shown above are dynamically queried. Select an extension tile above lớn read the mô tả tìm kiếm and đánh giá to decide which extension is best for you.

Start debugging

The following documentation is based on the built-in Node.js debugger, but most of the concepts & features are applicable khổng lồ other debuggers as well.

It is helpful to first create a sample Node.js application before reading about debugging. You can follow the Node.js walkthrough lớn install Node.js and create a simple "Hello World" JavaScript application (app.js). Once you have a simple application set up, this page will take you through VS debugging features.

Run và Debug view

To bring up the Run and Debug view, select the Run và Debug icon in the Activity Bar on the side of VS You can also use the keyboard shortcut ⇧⌘D (Windows, Linux Ctrl+Shift+D).


The Run và Debug view displays all information related khổng lồ running & debugging & has a đứng đầu bar with debugging commands & configuration settings.

If running và debugging is not yet configured (no launch.json has been created), VS shows the Run start view.


Run menu

The top-level Run menu has the most common run và debug commands:


Launch configurations

To run or debug a simple app in VS, select Run and Debug on the Debug start view or press F5 và VS will try khổng lồ run your currently active file.

However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to lớn configure & save debugging thiết lập details. VS keeps debugging configuration information in a launch.json tệp tin located in a folder in your workspace (project root folder) or in your user settings or workspace settings.

To create a launch.json file, click the create a launch.json file liên kết in the Run start view.


VS will try khổng lồ automatically detect your debug environment, but if this fails, you will have khổng lồ choose it manually:


Here is the launch configuration generated for Node.js debugging:

"version": "0.2.0", "configurations": < "type": "node", "request": "launch", "name": "Launch Program", "skipFiles": <"/**">, "program": "$workspaceFolder\app.js" >If you go back lớn the tệp tin Explorer view (⇧⌘E (Windows, Linux Ctrl+Shift+E)), you"ll see that VS has created a folder and added the launch.json file to your workspace.


Note: You can debug a simple application even if you don"t have a thư mục open in VS, but it is not possible khổng lồ manage launch configurations và set up advanced debugging. The VS Status Bar is purple if you vị not have a folder open.

Note that the attributes available in launch configurations vary from debugger to debugger. You can use IntelliSense suggestions (⌃Space (Windows, Linux Ctrl+Space)) lớn find out which attributes exist for a specific debugger. Hover help is also available for all attributes.

Do not assume that an attribute that is available for one debugger automatically works for other debuggers too. If you see green squiggles in your launch configuration, hover over them to lớn learn what the problem is and try to fix them before launching a debug session.


Review all automatically generated values and make sure that they make sense for your project và debugging environment.

Launch versus attach configurations

In VS, there are two vi xử lý core debugging modes, Launch & Attach, which handle two different workflows và segments of developers. Depending on your workflow, it can be confusing lớn know what type of configuration is appropriate for your project.

If you come from a browser Developer Tools background, you might not be used lớn "launching from your tool," since your browser instance is already open. When you mở cửa DevTools, you are simply attaching DevTools to lớn your mở cửa browser tab. On the other hand, if you come from a server or desktop background, it"s quite normal khổng lồ have your editor launch your process for you, & your editor automatically attaches its debugger khổng lồ the newly launched process.

The best way lớn explain the difference between launchattach is khổng lồ think of a launch configuration as a recipe for how to start your app in debug mode before VS attaches lớn it, while an attach configuration is a recipe for how to connect VS"s debugger to an phầm mềm or process that"s already running.

VS debuggers typically support launching a program in debug mode or attaching lớn an already running program in debug mode. Depending on the request (attach or launch), different attributes are required, & VS"s launch.json validation and suggestions should help with that.

Add a new configuration

To showroom a new configuration to an existing launch.json, use one of the following techniques:

Use IntelliSense if your cursor is located inside the configurations array.Press the Add Configuration button to invoke snippet IntelliSense at the start of the array.Choose Add Configuration option in the Run menu.


VS also supports compound launch configurations for starting multiple configurations at the same time; for more details, please read this section.

In order khổng lồ start a debug session, first select the configuration named Launch Program using the Configuration dropdown in the Run & Debug view. Once you have your launch configuration set, start your debug session with F5.

Alternatively, you can run your configuration through the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) by filtering on Debug: Select và Start Debugging or typing "debug " và selecting the configuration you want to debug.

As soon as a debugging session starts, the DEBUG CONSOLE panel is displayed & shows debugging output, and the Status Bar changes màu sắc (orange for default màu sắc themes):


In addition, the debug status appears in the Status Bar showing the active debug configuration. By selecting the debug status, a user can change the active launch configuration and start debugging without needing to mở cửa the Run và Debug view.


Debug actions

Once a debug session starts, the Debug toolbar will appear on the vị trí cao nhất of the editor.


Continue / Pause F5Continue: Resume normal program/script execution (up lớn the next breakpoint). Pause: Inspect executing at the current line and debug line-by-line.
Step Over F10Execute the next method as a single command without inspecting or following its component steps.
Step Into F11Enter the next method to follow its execution line-by-line.
Step Out ⇧F11 (Windows, Linux Shift+F11)When inside a method or subroutine, return lớn the earlier execution context by completing remaining lines of the current method as though it were a single command.
Restart ⇧⌘F5 (Windows, Linux Ctrl+Shift+F5)Terminate the current program execution và start debugging again using the current run configuration.
Stop ⇧F5 (Windows, Linux Shift+F5)Terminate the current program execution.

Tip: Use the setting debug.toolBarLocation lớn control the location of the debug toolbar. It can be the mặc định floating, docked to lớn the Run và Debug view, or hidden. A floating debug toolbar can be dragged horizontally và also down khổng lồ the editor area.

Run mode

In addition lớn debugging a program, VS supports running the program. The Debug: Run (Start Without Debugging) action is triggered with ⌃F5 (Windows, Linux Ctrl+F5) & uses the currently selected launch configuration. Many of the launch configuration attributes are supported in "Run" mode. VS maintains a debug session while the program is running, and pressing the Stop button terminates the program.

Tip: The Run kích hoạt is always available, but not all debugger extensions support "Run". In this case, "Run" will be the same as "Debug".


Breakpoints can be toggled by clicking on the editor margin or using F9 on the current line. Finer breakpoint control (enable/disable/reapply) can be done in the Run và Debug view"s BREAKPOINTS section.

Breakpoints in the editor margin are normally shown as red filled circles.Disabled breakpoints have a filled gray circle.When a debugging session starts, breakpoints that cannot be registered with the debugger change khổng lồ a gray hollow circle. The same might happen if the source is edited while a debug session without live-edit support is running.

If the debugger supports breaking on different kinds of errors or exceptions, those will also be available in the BREAKPOINTS view.

The Reapply All Breakpoints command sets all breakpoints again lớn their original location. This is helpful if your debug environment is "lazy" & "misplaces" breakpoints in source that has not yet been executed.


Optionally, breakpoints can be shown in the editor"s overview ruler by enabling the setting debug.showBreakpointsInOverviewRuler:



A Logpoint is a variant of a breakpoint that does not "break" into the debugger but instead logs a message to the console. Logpoints are especially useful for injecting logging while debugging production servers that cannot be paused or stopped.

A Logpoint is represented by a "diamond" shaped icon. Log messages are plain text but can include expressions khổng lồ be evaluated within curly braces ("").


Just lượt thích regular breakpoints, Logpoints can be enabled or disabled và can also be controlled by a condition and/or hit count.

Note: Logpoints are supported by VS"s built-in Node.js debugger, but can be implemented by other debug extensions. The Python và Java extensions, for example, support Logpoints.

Data inspection

Variables can be inspected in the VARIABLES section of the Run & Debug view or by hovering over their source in the editor. Variable values và expression evaluation are relative to the selected stack frame in the CALL STACK section.

Xem thêm: Mua Bán Sáo Trúc Mão Mèo Cực Đẹp, Giá Cực Tốt, Với Nhiều Ưu Đãi


Variable values can be modified with the Set Value action from the variable"s context menu. Additionally, you can use the Copy Value kích hoạt to copy the variable"s value, or Copy as Expression kích hoạt to copy an expression to lớn access the variable.

Variables and expressions can also be evaluated and watched in the Run and Debug view"s WATCH section.


Variable names and values can be filtered by typing while the focus is on the VARIABLES section.


Launch.json attributes

There are many launch.json attributes khổng lồ help tư vấn different debuggers & debugging scenarios. As mentioned above, you can use IntelliSense (⌃Space (Windows, Linux Ctrl+Space)) to lớn see the menu of available attributes once you have specified a value for the type attribute.


The following attributes are mandatory for every launch configuration:

type - the type of debugger khổng lồ use for this launch configuration. Every installed debug extension introduces a type: node for the built-in Node debugger, for example, or php và go for the PHP and Go extensions.request - the request type of this launch configuration. Currently, launch & attach are - the reader-friendly name to lớn appear in the Debug launch configuration dropdown.

Here are some optional attributes available lớn all launch configurations:

presentation - using the order, group, & hidden attributes in the presentation object, you can sort, group, and hide configurations và compounds in the Debug configuration dropdown và in the Debug quick pick.internalConsoleOptions - this attribute controls the visibility of the Debug Console panel during a debugging session.debugServer - for debug extension authors only: this attribute allows you lớn connect khổng lồ a specified port instead of launching the debug adapter.

Many debuggers tư vấn some of the following attributes:

program - executable or file to run when launching the debuggerargs - arguments passed to lớn the program lớn debugenv - environment variables (the value null can be used lớn "undefine" a variable)envFile - path to dotenv file with environment variablescwd - current working directory for finding dependencies và other filesport - port when attaching to a running processstopOnEntry - break immediately when the program launchesconsole - what kind of console to use, for example, internalConsole, integratedTerminal, or externalTerminal

Variable substitution

VS makes commonly used paths and other values available as variables and supports variable substitution inside strings in launch.json. This means that you vị not have to lớn use absolute paths in debug configurations. For example, $workspaceFolder gives the root path of a workspace folder, $file the file xuất hiện in the active editor, & $env:Name the environment variable "Name". You can see a full danh mục of predefined variables in the Variables Reference or by invoking IntelliSense inside the launch.json string attributes.

"type": "node", "request": "launch", "name": "Launch Program", "program": "$workspaceFolder/app.js", "cwd": "$workspaceFolder", "args": <"$env:USERNAME">

Platform-specific properties

Launch.json supports defining values (for example, arguments to lớn be passed to lớn the program) that depend on the operating system where the debugger is running. To vị so, put a platform-specific literal into the launch.json file & specify the corresponding properties inside that literal.

Below is an example that passes "args" to lớn the program differently on Windows:

"version": "0.2.0", "configurations": < "type": "node", "request": "launch", "name": "Launch Program", "program": "$workspaceFolder/node_modules/gulp/bin/gulpfile.js", "args": <"myFolder/path/app.js">, "windows": "args": <"myFolder\path\app.js"> >Valid operating properties are "windows" for Windows, "linux" for Linux, and "osx" for macOS. Properties defined in an operating system specific scope override properties defined in the global scope.

Please lưu ý that the type property cannot be placed inside a platform-specific section, because type indirectly determines the platform in remote debugging scenarios, and that would result in a cyclic dependency.

In the example below, debugging the program always stops on entry except on macOS:

"version": "0.2.0", "configurations": < "type": "node", "request": "launch", "name": "Launch Program", "program": "$workspaceFolder/node_modules/gulp/bin/gulpfile.js", "stopOnEntry": true, "osx": "stopOnEntry": false >

Global launch configuration

VS supports adding a "launch" object inside your User settings. This "launch" configuration will then be shared across your workspaces. For example:

"launch": "version": "0.2.0", "configurations": < "type": "node", "request": "launch", "name": "Launch Program", "program": "$file" >

Advanced breakpoint topics

Conditional breakpoints

A powerful VS debugging feature is the ability lớn set conditions based on expressions, hit counts, or a combination of both.

Expression condition: The breakpoint will be hit whenever the expression evaluates to lớn true.Hit count: The "hit count" controls how many times a breakpoint needs to lớn be hit before it will "break" execution. Whether a "hit count" is respected & the exact syntax of the expression vary among debugger extensions.

You can địa chỉ a condition and/or hit count when creating a source breakpoint (with the Add Conditional Breakpoint action) or when modifying an existing one (with the Edit Condition action). In both cases, an inline text box with a dropdown menu opens where you can enter expressions:


Condition và hit count editing tư vấn is also supported for functionexception breakpoints.You can initiate condition editing from the context menu or the new inline Edit Condition action.

An example of condition editing in the BREAKPOINTS view:


If a debugger does not tư vấn conditional breakpoints, the Add Conditional Breakpoint & Edit Condition actions will be missing.

Inline breakpoints

Inline breakpoints will only be hit when the execution reaches the column associated with the inline breakpoint. This is particularly useful when debugging minified which contains multiple statements in a single line.

An inline breakpoint can be set using ⇧F9 (Windows, Linux Shift+F9) or through the context menu during a debug session. Inline breakpoints are shown inline in the editor.

Inline breakpoints can also have conditions. Editing multiple breakpoints on a line is possible through the context menu in the editor"s left margin.

Function breakpoints

Instead of placing breakpoints directly in source, a debugger can tư vấn creating breakpoints by specifying a function name. This is useful in situations where source is not available but a function name is known.

A function breakpoint is created by pressing the + button in the BREAKPOINTS section header & entering the function name. Function breakpoints are shown with a red triangle in the BREAKPOINTS section.

Data breakpoints

If a debugger supports data breakpoints, they can be mix from the VARIABLES view & will get hit when the value of the underlying variable changes. Data breakpoints are shown with a red hexagon in the BREAKPOINTS section.

Debug Console REPL

Expressions can be evaluated with the Debug Console REPL (Read-Eval-Print Loop) feature. To open the Debug Console, use the Debug Console kích hoạt at the vị trí cao nhất of the Debug pane or use the View: Debug Console command (⇧⌘Y (Windows, Linux Ctrl+Shift+Y)). Expressions are evaluated after you press Enter và the Debug Console REPL shows suggestions as you type. If you need khổng lồ enter multiple lines, use Shift+Enter between the lines & then send all lines for evaluation with Enter.Debug Console đầu vào uses the mode of the active editor, which means that the Debug Console đầu vào supports syntax coloring, indentation, auto closing of quotes, & other language features.


Note: You must be in a running debug session lớn use the Debug Console REPL.

Redirect input/output to/from the debug target

Redirecting input/output is debugger/runtime specific, so VS does not have a built-in solution that works for all debuggers.

Here are two approaches you might want khổng lồ consider:

Launch the program to debug ("debug target") manually in a terminal or command prompt & redirect input/output as needed. Make sure lớn pass the appropriate command line options khổng lồ the debug target so that a debugger can attach khổng lồ it. Create và run an "attach" debug configuration that attaches lớn the debug target.

If the debugger extension you are using can run the debug target in VS"s Integrated Terminal (or an external terminal), you can try lớn pass the shell redirect syntax (for example, "") as arguments.

Here"s an example launch.json configuration:

"name": "launch program that reads a tệp tin from stdin", "type": "node", "request": "launch", "program": "program.js", "console": "integratedTerminal", "args": <", "in.txt">This approach requires that the "The individual sessions now show up as top-level elements in the CALL STACK view.

The debug toolbar shows the currently active session (and all other sessions are available in a dropdown menu).
Debug actions (for example, all actions in the debug toolbar) are performed on the active session. The active session can be changed either by using the dropdown thực đơn in the debug toolbar or by selecting a different element in the CALL STACK view.

Compound launch configurations

An alternative way to lớn start multiple debug sessions is by using a compound launch configuration. A compound launch configuration lists the names of two or more launch configurations that should be launched in parallel. Optionally a preLaunchTask can be specified that is run before the individual debug sessions are started. The boolean flag stopAll controls whether manually terminating one session will stop all of the compound sessions.

"version": "0.2.0", "configurations": < "type": "node", "request": "launch", "name": "Server", "program": "$workspaceFolder/server.js" , "type": "node", "request": "launch", "name": "Client", "program": "$workspaceFolder/client.js" >, "compounds": < "name": "Server/Client", "configurations": <"Server", "Client">, "preLaunchTask": "$defaultBuildTask", "stopAll": true >Compound launch configurations are displayed in the launch configuration dropdown menu.

Remote debugging

VS does not itself tư vấn remote debugging: this is a feature of the debug extension you are using, & you should consult the extension"s page in the Marketplace for tư vấn and details.

There is, however, one exception: the Node.js debugger included in VS supports remote debugging. See the Node.js Debugging topic khổng lồ learn how to configure this.

Automatically mở cửa a URI when debugging a vps program

Developing a website program typically requires opening a specific URL in a web browser in order lớn hit the vps in the debugger. VS has a built-in feature "serverReadyAction" lớn automate this task.

Here is an example of a simple Node.js Express application:

var express = require('express');var phầm mềm = express();app.get('/', function(req, res) res.send('Hello World!'););app.listen(3000, function() console.log('Example app listening on port 3000!'););This application first installs a "Hello World" handler for the "/" URL and then starts lớn listen for HTTP connections on port 3000. The port is announced in the Debug Console, và typically, the developer would now type http://localhost:3000 into their browser application.

The serverReadyAction feature makes it possible to add a structured property serverReadyAction to any launch config & select an "action" lớn be performed:

"type": "node", "request": "launch", "name": "Launch Program", "program": "$workspaceFolder/app.js", "serverReadyAction": "pattern": "listening on port (<0-9>+)", "uriFormat": "http://localhost:%s", "action": "openExternally" Here the pattern property describes the regular expression for matching the program"s đầu ra string that announces the port. The pattern for the port number is put into parenthesis so that it is available as a regular expression capture group. In this example, we are extracting only the port number, but it is also possible khổng lồ extract a full URI.

The uriFormat property describes how the port number is turned into a URI. The first %s is substituted by the first capture group of the matching pattern.

The resulting URI is then opened outside of VS ("externally") with the standard application configured for the URI"s scheme.

Trigger Debugging via Edge or Chrome

Alternatively, the kích hoạt can be set khổng lồ debugWithEdge or debugWithChrome. In this mode, a webRoot property can be added that is passed lớn the Chrome or Edge debug session.

To simplify things a bit, most properties are optional và we use the following fallback values:

pattern: "listening on.* (https?://\S+|<0-9>+)" which matches the commonly used messages "listening on port 3000" or "Now listening on: https://localhost:5001".uriFormat: "http://localhost:%s"webRoot: "$workspaceFolder"

Triggering an Arbitrary Launch Config

In some cases, you may need khổng lồ configure additional options for the browser debug session--or use a different debugger entirely. You can do this by setting kích hoạt to startDebugging with a name property set to lớn the name of the launch configuration to start when the pattern is matched.

The named launch configuration must be in the same file or thư mục as the one with the serverReadyAction.

Here the serverReadyAction feature in action:


Next steps

To learn about VS"s Node.js debugging support, take a look at:

To see tutorials on the basics of Node.js debugging, kiểm tra out these videos:

To learn about debugging tư vấn for other programming languages via VS extensions:

To learn about VS"s task running support, go to:

To write your own debugger extension, visit:

Common questions

What are the supported debugging scenarios?

Debugging of Node.js-based applications is supported on Linux, macOS, & Windows out of the box with VS Many other scenarios are supported by VS extensions available in the Marketplace.

I vì not see any launch configurations in the Run and Debug view dropdown. What is wrong?

The most common problem is that you did not phối up launch.json or there is a syntax error in that file. Alternatively, you might need to xuất hiện a folder, since no-folder debugging does not support launch configurations.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • 7 lần nàng tiên cá xuất hiện ngoài đời thực được con người bắt gặp i vivu thế giới

  • Xe gtdd nghĩa la gì

  • Chịu nổi hay chịu nỗi

  • Cấu trúc mệnh đề if

  • x

    Welcome Back!

    Login to your account below

    Retrieve your password

    Please enter your username or email address to reset your password.