Enable C++ Development in VS Code on macOS (Clang + LLDB)
This guide walks you through a clean, step-by-step setup to compile and debug C++ in Visual Studio Code on macOS using Clang and LLDB.
This guide works on:
- macOS Ventura
- macOS Sonoma
- macOS Monterey
- Apple Silicon (M1/M2/M3)
- Intel Macs
Note: Visual Studio Code does not include a compiler. Clang is provided via the Xcode Command Line Tools.
Prerequisites
- VS Code installed: Download from the official site and install the app.
- Basic terminal access: You’ll use the macOS Terminal for a few commands.
Step 1: Install the C/C++ extension
- Open VS Code.
- Go to Extensions: Press
Shift+Cmd+X. - Search and install: Find C/C++ by Microsoft and click Install.
- Restart VS Code: Optional but helps the extension initialize cleanly.
Step 2: Install the C++ compiler and tools (Clang/LLVM via Xcode CLT)
- Open Terminal (Applications → Utilities → Terminal).
- Install Xcode Command Line Tools:
xcode-select --install
- Accept prompts and wait for installation to finish.
- Verify Clang is available:
clang++ --version
You should see a version output including Apple clang version.
Step 3: Create a test C++ project
- Create a project folder:
mkdir ~/cpp-hello && cd ~/cpp-hello
⚠️ If the code command is not found
Some macOS systems don’t have the VS Code command-line launcher enabled by default.
Enable it in two quick steps:
- Open VS Code.
- Press
Cmd+Shift+P→ type shell command → select Shell Command: Install ‘code’ command in PATH.
After that, the command below will work.
- Open the folder in VS Code:
code .
- Create a source file: In VS Code, add a new file named
main.cpp. - Add sample code:
#include <iostream>
int main() {
std::cout << "Hello, C++ from macOS!" << std::endl;
return 0;
}
Step 4: Configure build tasks (tasks.json)
- Create a .vscode folder: In your project, create
.vscode/. - Add
tasks.json: Create.vscode/tasks.jsonwith:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C++: Build with clang++",
"command": "clang++",
"args": [
"-std=c++17",
"-g",
"main.cpp",
"-o",
"build/main"
],
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
]
}
]
}
- Create build directory: Optional but clean.
mkdir -p build
Notes:
-std=c++17: Adjust to your preferred standard (c++20, etc.).-g: Includes debug symbols for LLDB.
Step 5: Configure debugging (launch.json)
- Add
launch.json: Create.vscode/launch.jsonwith:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++: Debug build/main (LLDB)",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/build/main",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "C++: Build with clang++",
"stopOnEntry": false,
"environment": [],
"console": "integratedTerminal"
}
]
}
Step 6: Configure IntelliSense (optional but recommended)
- Add
c_cpp_properties.json: Create.vscode/c_cpp_properties.json:
{
"version": 4,
"configurations": [
{
"name": "macOS",
"compilerPath": "/usr/bin/clang++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-arm64"
}
]
}
- Apple Silicon vs. Intel:
- Apple Silicon:
macos-clang-arm64 - Intel:
macos-clang-x64
Step 7: Build and run
- Build: Press
Cmd+Shift+B(or Terminal → Run Build Task). You should seebuild/maincreated. - Run in terminal:
./build/main
Output should be:
Hello, C++ from macOS!
- Debug in VS Code:
- Go to Run and Debug (
Cmd+Shift+D). - Select C++: Debug build/main (LLDB).
- Click Start. Set breakpoints in
main.cppand step through.
Step 8: Add multiple files (optional)
If you add more .cpp files, either list them in args or use a wildcard with a simple
build script:
Update tasks.json args:
"args": ["-std=c++17", "-g", "*.cpp", "-o", "build/main"]
For larger projects, consider a Makefile or CMake.
Step 9: Common issues and fixes
- Clang not found:
- Run the installer again:
xcode-select --install- Check path:
which clang++ - Permission denied running
build/main:- Ensure executable bit is set (usually automatic). If not:
chmod +x build/main - IntelliSense errors not matching compiler:
- Ensure
compilerPathpoints to/usr/bin/clang++. - Align
cppStandardwith your flags (c++17,c++20).
- Ensure
- Debugging doesn’t start or skips breakpoints:
- Confirm
-gis in build flags. - Make sure
programpath inlaunch.jsonmatches the output binary.
- Confirm
- Apple Silicon architecture mismatches:
- Keep
intelliSenseModeasmacos-clang-arm64. - Avoid mixing Homebrew Intel toolchains with Apple Silicon builds.
- Keep
Step 10: Optional enhancements
- Format code automatically: Install “Clang-Format” and set
"C_Cpp.clang_format_style": "file"with a.clang-formatfile. - Use CMake Tools: For larger projects, install “CMake Tools” and initialize a CMake project for robust multi-file builds.
- Unit testing: Add frameworks like Catch2 or GoogleTest and include them in your build.
Quick reference
- Build:
Cmd+Shift+B - Run debug:
Cmd+Shift+D→ Start - Compiler:
/usr/bin/clang++ - Debugger: LLDB (built into macOS)