From 2698f7f60bcfe117ebb85fc1301d15dfc8640eca Mon Sep 17 00:00:00 2001 From: Tyler Beckman Date: Thu, 19 Sep 2024 19:36:01 -0600 Subject: [PATCH] Overhaul makefile, add include guards --- Makefile | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++----- math.h | 7 ++++++- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index a013895..3b67805 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,69 @@ TARGET = L1A SRC_FILES = main.cpp math.cpp -# NO EDITS BELOW THIS LINE +# Tyler's custom makefile extensions +.DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all" +.PHONY: pack clean-run c run + +## Adds only the necessary files for build into a .tar.gz file, named appropriately +ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp) +pack: + tar --ignore-failed-read -czvf $(TARGET).tar.gz $(shell echo $(ARCHIVED_FILES) | xargs ls -d 2>/dev/null) + +## Runs the pack target and then attempts to build & run the program to make sure it functions correctly +pack-test: pack + $(eval TMP := $(shell mktemp -d)) + tar -xvzf $(TARGET).tar.gz --directory $(TMP) + make -C $(TMP) + $(TMP)/$(TARGET) + rm -rf $(TMP) + +## An extension of the clean command that is shorter to type and removes a potential .tar.gz file +c: clean + $(DEL) -f $(TARGET).tar.gz + +## Simply builds and then executes the program +run: all + ./$(TARGET) + +# NO EDITS NEEDED BELOW THIS LINE + CXX = g++ +CXXFLAGS = -O2 +CXXFLAGS_DEBUG = -g +CXXFLAGS_WARN = -Wall -Wextra -Wunreachable-code -Wshadow -Wpedantic +CPPVERSION = -std=c++17 + OBJECTS = $(SRC_FILES:.cpp=.o) ifeq ($(shell echo "Windows"), "Windows") TARGET := $(TARGET).exe DEL = del + Q = else DEL = rm -f + Q = " endif all: $(TARGET) $(TARGET): $(OBJECTS) - $(CXX) -std=c++17 -o $@ $^ + $(CXX) -o $@ $^ -%.o: %.cpp - $(CXX) -std=c++17 -o $@ -c $< +.cpp.o: + $(CXX) $(CXXFLAGS) $(CPPVERSION) $(CXXFLAGS_DEBUG) $(CXXFLAGS_WARN) -o $@ -c $< clean: - $(DEL) $(TARGET) $(OBJECTS) \ No newline at end of file + $(DEL) -f $(TARGET) $(OBJECTS) Makefile.bak + +depend: + @sed -i.bak '/^# DEPENDENCIES/,$$d' Makefile + @$(DEL) sed* + @echo $(Q)# DEPENDENCIES$(Q) >> Makefile + @$(CXX) -MM $(SRC_FILES) >> Makefile + +.PHONY: all clean depend + +# DEPENDENCIES +main.o: main.cpp math.h +math.o: math.cpp diff --git a/math.h b/math.h index 32130f6..948d2c8 100644 --- a/math.h +++ b/math.h @@ -1,3 +1,6 @@ +#ifndef MATH_H +#define MATH_H + double ideal_gas_law(double, double, double); double average_acceleration(double, double, double, double); @@ -18,4 +21,6 @@ double stress(double, double); double shear_stress(double, double, double, double); -double coulombs_law(double, double, double, double); \ No newline at end of file +double coulombs_law(double, double, double, double); + +#endif // MATH_H