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

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 .

clean:
    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

clean:
    rm TestTime

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

References

  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

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.