Created
January 27, 2023 17:55
-
-
Save michaelhelvey/e08ce78324fa4c7a01e4889338cfeaf9 to your computer and use it in GitHub Desktop.
Simple makefile for C/CPP/ASM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HT Job Vranish: https://spin.atomicobject.com/2016/08/26/makefile-c-projects/ | |
TARGET_EXEC ?= target.out | |
BUILD_DIR ?= ./build | |
SRC_DIRS ?= ./src | |
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s) | |
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) | |
DEPS := $(OBJS:.o=.d) | |
INC_DIRS := $(shell find $(SRC_DIRS) -type d) | |
INC_FLAGS := $(addprefix -I,$(INC_DIRS)) | |
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP | |
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS) | |
$(CC) $(OBJS) -o $@ $(LDFLAGS) | |
# assembly | |
$(BUILD_DIR)/%.s.o: %.s | |
$(MKDIR_P) $(dir $@) | |
$(AS) $(ASFLAGS) -c $< -o $@ | |
# c source | |
$(BUILD_DIR)/%.c.o: %.c | |
$(MKDIR_P) $(dir $@) | |
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ | |
# c++ source | |
$(BUILD_DIR)/%.cpp.o: %.cpp | |
$(MKDIR_P) $(dir $@) | |
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ | |
.PHONY: clean | |
clean: | |
$(RM) -r $(BUILD_DIR) | |
.PHONY: run | |
run: $(BUILD_DIR)/$(TARGET_EXEC) | |
@$(BUILD_DIR)/$(TARGET_EXEC) | |
-include $(DEPS) | |
MKDIR_P ?= mkdir -p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment