Chuyển tới nội dung chính

C/C++ Installation

I. What do we need?​

1/ Editor vs Compiler​

  • VS Code is the editor (where u write code), the compiler turns ur code into a program
  • on mac we use clang, apple's own compiler. it comes with the Command Line Tools (no need for the full Xcode app)
  • heads up: on mac, gcc / g++ actually run clang under the hood. fine for most things, not for everything (see IV)

2/ Before u start​

  • a few GB free on ur disk
  • ur mac password (or touch id) for the installs

II. Installing​

1/ Visual Studio Code​

  • go to: https://code.visualstudio.com/
  • click on Download Mac Universal (works on both intel and apple silicon macs), it's a .zip file
  • unzip it (safari usually does that for u)
  • drag Visual Studio Code into the Applications folder in Finder
  • bonus: open VS Code -> Cmd + Shift + P -> Shell Command: Install 'code' command in PATH. now code . in the terminal opens the current folder in VS Code

2/ Command Line Tools (clang)​

  • open Terminal (Cmd + Space -> type Terminal -> Enter)
  • paste this in and press Enter:
xcode-select --install
  • a popup will ask to install the command line developer tools -> Install -> Agree, then wait (a few minutes depending on ur internet)
  • if it says command line tools are already installed, ure already good

3/ Check clang​

  • still in Terminal:
clang --version
clang++ --version
  • if the output is, for example, Apple clang version ... then ure done

4/ C/C++ extension for VS Code​

  • Visual Studio Code -> Cmd + Shift + X (Extensions)
  • search for C/C++
  • select and install C/C++ (Microsoft)

III. First program​

1/ Create a folder​

  • make a folder for ur code, e.g. ~/code/cpp
  • avoid spaces and Vietnamese characters in the path, keeps the compiler and the debugger happy
  • open it in VS Code: File > Open Folder...

2/ hello.cpp​

  • create a file hello.cpp:
hello.cpp
#include <iostream>
using namespace std;

int main() {
cout << "hello from c++" << endl;
return 0;
}

3/ Compile and run​

  • open the terminal inside VS Code: Ctrl + `
  • compile:
clang++ -std=c++17 hello.cpp -o hello
  • run:
./hello
  • for C, use clang: clang hello.c -o hello
  • always set the standard yourself (-std=c++17, -std=c++20), so the code behaves the same on every machine. add -Wall to see warnings

IV. Common errors​

  • fatal error: 'bits/stdc++.h' file not found: that header only exists in gcc, clang doesn't have it. include what u need (<iostream>, <vector>,...), or install the real gcc (below)
  • real gcc via Homebrew (optional): install Homebrew first (the command is also on brew.sh), then gcc:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install gcc
  • homebrew's gcc comes with a version number, e.g. g++-15 (plain g++ is still clang). see which one u got: ls $(brew --prefix)/bin | grep g++
  • xcrun: error: invalid active developer path: usually right after a macOS update. just run xcode-select --install again
  • zsh: command not found: hello: run it with ./ in front: ./hello

So overall: Command Line Tools give u clang, VS Code to write, the terminal to run. need bits/stdc++.h? get gcc from Homebrew​


V. Reference Sources​

  • Visual Studio Code Docs. Using Clang in Visual Studio Code. code.visualstudio.com
  • Homebrew. brew.sh