In this tutorial, you will configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger on Linux. GCC stands for GNU Compiler Collection; GDB is the GNU debugger. Show
After configuring VS Code, you will compile and debug a simple C++ program in VS Code. This tutorial does not teach you GCC, GDB, Ubuntu or the C++ language. For those subjects, there are many good resources available on the Web. If you have trouble, feel free to file an issue for this tutorial in the VS Code documentation repository. PrerequisitesTo successfully complete this tutorial, you must do the following:
Ensure GCC is installedAlthough you'll use VS Code to edit your source code, you'll compile the source code on Linux using the g++ compiler. You'll also use GDB to debug. These tools are not installed by default on Ubuntu, so you have to install them. Fortunately, that's easy. First, check to see whether GCC is already installed. To verify whether it is, open a Terminal window and enter the following command:
If GCC isn't installed, run the following command from the terminal window to update the Ubuntu package lists. An out-of-date Linux distribution can sometimes interfere with attempts to install new packages.
Next install the GNU compiler tools and the GDB debugger with this command:
Create Hello WorldFrom the terminal window, create an empty folder called 0 to store your VS Code projects. Then create a subfolder called 1, navigate into it, and open VS Code in that folder by entering the following commands:
The 2 command opens VS Code in the current working folder, which becomes your "workspace". As you go through the tutorial, you will create three files in a 3 folder in the workspace:
Add hello world source code fileIn the File Explorer title bar, select New File and name the file 7.Paste in the following source code:
Now press ⌘S (Windows, Linux Ctrl+S) to save the file. Notice that your files are listed in the File Explorer view (⇧⌘E (Windows, Linux Ctrl+Shift+E)) in the side bar of VS Code: You can also enable to automatically save your file changes, by checking Auto Save in the main File menu. The Activity Bar on the edge of Visual Studio Code 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.
Explore IntelliSenseIn the 7 file, hover over 0 or 1 to see type information. After the declaration of the 2 variable, start typing 3as 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 2 object:You can press the Tab key to insert the selected member. Then, when you add the opening parenthesis, you'll see information about arguments that the function requires. Run helloworld.cppRemember, 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 7 in VS Code.
You'll only be asked to choose a compiler the first time you run 7. This compiler will be set as the "default" compiler in 4 file.
The first time you run your program, the C++ extension creates 4, which you'll find in your project's 3 folder. 4 stores build configurations.Your new 4 file should look similar to the JSON below:
The 4 setting specifies the program to run; in this case that is g++. The 5 array specifies the command-line arguments that will be passed to g++. These arguments must be specified in the order expected by the compiler.This task tells g++ to take the active file ( 6), compile it, and create an executable file in the current directory ( 7) with the same name as the active file but without an extension ( 8), resulting in 1 for our example.The 0 value is what you will see in the tasks list; you can name this whatever you like.The 1 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.From now on, the play button will read from 4 to figure out how to build and run your program. You can define multiple build tasks in 4, and whichever task is marked as the default will be used by the play button. In case you need to change the default compiler, you can run Tasks: Configure default build task. Alternatively you can modify the 4 file and remove the default by replacing this segment:
with this:
Modifying tasks.jsonYou can modify your 4 to build multiple C++ files by using an argument like 6 instead of 6.This will build all 8 files in your current folder. You can also modify the output filename by replacing 9 with a hard-coded filename (for example 'helloworld.out').Debug helloworld.cpp
The play button has two modes: Run C/C++ File and Debug C/C++ File. It will default to the last-used mode. If you see the debug icon in the play button, you can just click the play button to debug, instead of selecting the drop-down menu item. Explore the debuggerBefore you start stepping through the code, let's take a moment to notice several changes in the user interface:
If you already have a launch.json file in your workspace, the play button will read from it when figuring out how run and debug your C++ file. If you don’t have launch.json, the play button will create a temporary “quick debug” configuration on the fly, eliminating the need for launch.json altogether! Step through the codeNow you're ready to start stepping through the code.
Set a watchTo keep track of the value of a variable as your program executes, set a watch on the variable.
Next, you'll create a 4 file to tell VS Code how to build (compile) the program. This task will invoke the g++ compiler to create an executable file from the source code.It's important to have 7 open in the editor because the next step uses the active file in the editor for context to create the build task in the next step.Customize debugging with launch.jsonWhen you debug with the play button or F5, the C++ extension creates a dynamic debug configuration on the fly. 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 5 file.To create 5, choose Add Debug Configuration from the play button drop-down menu.You'll then see a dropdown for various predefined debugging configurations. Choose g++ build and debug active file. VS Code creates a 5 file, which looks something like this:
In the JSON above, 5 specifies the program you want to debug. Here it is set to the active file folder 7 and active filename without an extension 8, which if 7 is the active file will be 1. The 5 property is an array of arguments to pass to the program at runtime.By default, the C++ extension won't add any breakpoints to your source code and the 1 value is set to 2.Change the 1 value to 4 to cause the debugger to stop on the 5 method when you start debugging.
C/C++ configurationsIf you want more control over the C/C++ extension, you can create a 6 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)). This opens the C/C++ Configurations page. When you make changes here, VS Code writes them to a file called 6 in the 3 folder.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 0. If you open that file directly, it should look something like this:
Reusing your C++ configurationVS Code is now configured to use gcc on Linux. The configuration applies to the current workspace. To reuse the configuration, just copy the JSON files to a 3 folder in a new project folder (workspace) and change the names of the source file(s) and executable as needed.TroubleshootingCompiler and linking errorsThe most common cause of errors (such as 2, or 3, and so on) occurs when 7 is not the active file when you start a build or start debugging. This is because the compiler is trying to compile something that isn't source code, like your 5, 4, or 6 file. |