Advertisement
vlatkovski

Good Makefile

Feb 16th, 2022
875
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # credits:
  2. # https://www.youtube.com/watch?v=noBh17uxQ1E
  3.  
  4. # compiler and linker flags
  5. CC        := g++
  6. SRC_DIR   := src
  7. BIN_DIR   := bin
  8. BIN_EXE   := $(BIN_DIR)/main
  9. OUTPUT    := $(if $(findstring Windows_NT, $(OS)), $(BIN_EXE).exe, $(BIN_EXE).out)
  10. OBJ_DIR   := $(BIN_DIR)/obj
  11. INC_DIRS  := -I$(SRC_DIR)
  12. LIB_DIRS  :=
  13. SRC_FILES := $(wildcard $(SRC_DIR)/*.cpp)
  14. H_FILES   := $(wildcard $(SRC_DIR)/*.h)
  15. OBJ_FILES := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRC_FILES))
  16. CPP_FLAGS := -std=c++17
  17. LD_FLAGS  :=
  18. MAKEFLAGS += -j8
  19.  
  20. # compile the object files and place them in their own directory
  21. $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(H_FILES)
  22.     $(CC) $(CPP_FLAGS) $(INC_DIRS) -c -o $@ $<
  23.  
  24. # link the object files together to create the final executable
  25. $(OUTPUT): $(OBJ_FILES) Makefile
  26.     $(CC) $(LIB_DIRS) $(LD_FLAGS) $(OBJ_FILES) -o $(OUTPUT)
  27.  
  28. # when we type make, compile and link the executable
  29. all: $(OUTPUT)
  30.  
  31. # if we type 'make run' it will build and run the executable
  32. run: $(OUTPUT)
  33.     $(if $(findstring Windows_NT, $(OS)), cd bin && main.exe && cd .., cd bin && ./main.out && cd ..)
  34.  
  35. # if we type 'make clean' it will clean up all the object files and the executable
  36. clean:
  37.     $(if $(findstring Windows_NT, $(OS)), del bin\obj\*.o && del bin\main.exe, rm $(OBJ_DIR)/*.o && rm $(OUTPUT))
  38.  
Advertisement
RAW Paste Data Copied
Advertisement