Setting Up VS Code to Compile and Run C++ (Mac)

What You’ll Need

  • Visual Studio Code
  • C++ compiler (clang for Mac)
  • VS Code C++ extensions

1. Install Xcode Command Line Tools (includes clang)

Open Terminal and run:

xcode-select --install

Follow the prompt to install the tools.


2. Install Visual Studio Code

Download and install from:
https://code.visualstudio.com


3. Install C++ Extension in VS Code

  1. Open VS Code
  2. Go to the Extensions tab (left sidebar or Cmd+Shift+X)
  3. Search for and install:
    • C/C++ by Microsoft

4. Create and Run a C++ Program

  1. Create a new folder for your project (e.g., cpp-labs)
  2. Open the folder in VS Code:
    FileOpen Folder
  3. Create a new file: hello.cpp

Paste the following:

#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
  1. Open the Terminal in VS Code:
    TerminalNew Terminal
  2. Compile the code:
clang++ hello.cpp -o hello
  1. Run the program:
./hello

You should see:

Hello, world!Hello, world!


Scroll to Top