Home / Programming / Programming Languages / C++ / Compile and link C++ source codes and headers from terminal in MacOS

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. https://stackoverflow.com/questions/27436981/xcode-c-duplicate-symbols-for-architecture-x86-64 accessed on 06/06/2020
  2. https://www.cs.fsu.edu/~myers/c++/notes/compilation.html accessed on 07/06/2020
  3. https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html accessed on 08/06/2020
  4. https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html accessed on 08/06/2020

About Nguyen Vu Ngoc Tung

I love making new professional acquaintances. Don't hesitate to contact me via nguyenvungoctung@gmail.com if you want to talk about information technology, education, and research on complex networks analysis (i.e., metabolic networks analysis), data analysis, and applications of graph theory. Specialties: researching and proposing innovative business approaches to organizations, evaluating and consulting about usability engineering, training and employee development, web technologies, software architecture.

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.