Basic Makefile C++

CXX = g++
CXXFLAGS = -std=c++11 -Wall

all: main

main: main.o
    $(CXX) $(CXXFLAGS) -o main main.o

main.o: main.cpp
    $(CXX) $(CXXFLAGS) -c main.cpp

clean:
    rm -f main main.o
  1. CXX = g++: Defines the variable CXX as the g++ compiler.
  2. CXXFLAGS = -std=c++11 -Wall: Defines the variable CXXFLAGS as the compilation flags, including the C++11 standard and enabling all compiler warnings.
  3. all: main: Declares the target "all" as dependent on the target "main".
  4. main: main.o: Declares the target "main" as dependent on the file "main.o".
  5. $(CXX) $(CXXFLAGS) -o main main.o: Specifies the command to link the object file "main.o" into the executable "main" using the g++ compiler with the defined compilation flags.
  6. main.o: main.cpp: Declares the target "main.o" as dependent on the source file "main.cpp".
  7. $(CXX) $(CXXFLAGS) -c main.cpp: Specifies the command to compile the source file "main.cpp" into the object file "main.o" using the g++ compiler with the defined compilation flags.
  8. clean: Declares the target "clean" as a phony target, which removes the executable "main" and the object file "main.o" when invoked using the command "make clean".