Using Clang in Visual Studio Code
AI agents: See llms.txt for documentation discovery and navigation. Documentation articles are also available as Markdown by appending .md to the article URL. 🎬 Watch The Story of VS Code! VS Code is now on Instagram, follow @vscode.ig for updates, tips, and more. Rewatch GitHub Copilot Day. Using Clang in Visual Studio Code View as Markdown Ask in VS Code Ask in VS Code Insiders In this tutorial, you configure Visual Studio Code on macOS to use the Clang/LLVM compiler and debugger.
After configuring VS Code, you will compile and debug a C++ program in VS Code. This tutorial does not teach you about Clang or the C++ language. For those subjects, there are many good resources available on the Web. If you have any trouble, 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 steps: Install Visual Studio Code on macOS .
Install the 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 ) ). Ensure Clang is installed Clang might already be installed on your Mac. To verify that it is, open a macOS Terminal window and enter the following command: clang --version If Clang isn't installed, enter the following command to install the command line developer tools, which include Clang: xcode-select --install Create Hello World app From the macOS Terminal, 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 in that folder by entering the following commands in the terminal window: mkdir projects cd projects mkdir helloworld cd helloworld code .
The code . command opens VS Code in the current working folder, which becomes your "workspace". As you go through the tutorial, three files are created in the .vscode folder in your workspace: tasks.json (compiler build settings) launch.json (debugger settings) c_cpp_properties.json (compiler path and IntelliSense settings) Add a hello world source code file In the File Explorer title bar, select the New File button and name the file helloworld.cpp .
Paste in the following source code: #include <iostream> #include <vector> #include <string> using namespace std ; int main () { vector<string> msg { "Hello" , "C++" , "World" , "from" , "VS Code" , "and the C++ extension!" }; for ( const string& word : msg) { cout << word << " " ; } cout << endl; } 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 Auto Save to automatically save your file changes, by selecting File > Auto Save .
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 IntelliSense is a tool to help you code faster and more efficiently by adding code editing features such as code completion, parameter info, quick info, and member lists.
To see IntelliSense in action, hover over vector or string to see their type information. If you type msg. in line 10, you can see a completion list of recommended member functions to call, all generated by IntelliSense: You can press the Tab key to insert the selected member. Then, when you add the opening parenthesis, information about arguments that the function requires is displayed. If IntelliSense is not already configured, open the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ) and enter Select IntelliSense Configuration .
From the dropdown of compilers, select Use clang++ to configure. More information can be found in the IntelliSense configuration documentation . Run helloworld.cpp Remember, the C++ extension uses the C++ compiler you installed on your machine to build your program. Make sure you have a C++ compiler, such as Clang, 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++: clang++ build and debug active file from the list of detected compilers on your system. You are only asked to choose a compiler the first time you run helloworld.cpp . This compiler is the "default" compiler set in tasks.json file. After the build succeeds, your program's output will appear in the integrated Debug Console . Congratulations! You've just run your first C++ program in VS Code!
Understanding tasks.json The first time you run your program, the C++ extension creates tasks.json , located in your project's .vscode folder. tasks.json stores build configurations. Here is a sample of a tasks.json file on macOS: { "tasks" : [ { "type" : "cppbuild" , "label" : "C/C++: clang++ build active file" , "command" : "/usr/bin/clang++" , "args" : [ "-fcolor-diagnostics" , "-fansi-escape-codes" , "-g" , "${file}" , "-o" , "${fileDirname}/${fileBasenameNoExtension}" ], "options" : { "cwd" : "${fileDirname}" }, "problemMatcher" : [ "$gcc" ], "group" : { "kind" : "build" , "isDefault" : true }, "detail" : "Task generated by Debugger." } ], "version" : "2.0.0" } 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 clang++ . The args array specifies the command-line arguments that are passed to clang++. These arguments must be specified in the order expected by the compiler. This task tells the C++ compiler to take the active file ( ${file} ), compile it, and create an output file ( -o switch) in the current directory ( ${fileDirname} ) with the same name as the active file but without the file extension ( ${fileBasenameNoExtension} ).
This process creates helloworld . The label value is what you see in the tasks list, which is based on your personal preference. The detail value is the description of the task in the tasks list. Update this string to differentiate it from similar tasks. The problemMatcher value selects the output parser to use for finding errors and warnings in the compiler output. For clang++, the $gcc problem matcher creates the best results.
From now on, the play button always reads from tasks.json to figure out how to build and run your program. You can define multiple build tasks in tasks.json , and whichever task is marked as the default is the one used by the play button. In case you need to change the default compiler, you can run Tasks: Configure Default Build Task in the Command Palette. Alternatively you can modify the tasks.json file and remove the default by replacing this segment: "group" : { "kind" : "build" , "isDefault" : true }, with this: "group" : "build" , Modifying tasks.json You can modify your tasks.json to build multiple C++ files by using an argument like "${workspaceFolder}/*.cpp" instead of "${file}" .
This builds all .cpp files in your current folder. You can also modify the output filename by replacing "${fileDirname}/${fileBasenameNoExtension}" with a hard-coded filename (for example "${workspaceFolder}/myProgram.out" ). Debug helloworld.cpp To debug your code, Go back to helloworld.cpp so that it is the active file. Set a breakpoint by clicking on the editor margin or using F9 on the current line. From the drop-down next to the play button, select Debug C/C++ File .
Choose C/C++: clang++ build and debug active file from the list of detected compilers on your system (you'll only be asked to choose a compiler the first time you run or debug helloworld.cpp ). You will see the task execute and print out the output to the Terminal window. The play button has two modes: Run
The rest of this page is on the live site.