Compile and link C++ source codes and headers from terminal in MacOS

Print Friendly, PDF & Email

Compiling C++ source codes and headers files from Terminal in macOS is a bit painful if you don’t have any experience with compiling.

What I have learned below.

Create a Makefile file

all: foo.h foo.cpp main.cpp
    g++ -Wall -o main.out main.cpp -I .

    rm main.out

I found another case working that I don’t understand why

all: Timer.h Timer.cpp TestTime.cpp
    g++ -Wall -Wextra -o TestTime Timer.cpp TestTime.cpp -I . -v

    rm TestTime

This Makefile requires the TestTime.cpp while the former file does not.


  1. accessed on 06/06/2020
  2. accessed on 07/06/2020
  3. accessed on 08/06/2020
  4. accessed on 08/06/2020

Leave a Reply

Your email address will not be published. Required fields are marked *



This site uses Akismet to reduce spam. Learn how your comment data is processed.