Last active
February 25, 2025 12:30
-
-
Save rezan/63e92fcc86c8eb50ec470a568d1110f9 to your computer and use it in GitHub Desktop.
Makefile with auto dependencies, source subdirs, and a build directory
This file contains hidden or 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
# Makefile with auto dependencies, source subdirs, and a build directory | |
# Binary name | |
NAME=test | |
CC=gcc | |
CFLAGS=-g -Wall -I. | |
DEPFLAGS=-MMD -MP | |
LDFLAGS= | |
LIBFLAGS= | |
BUILD_DIR=build | |
SOURCE:=$(shell find * -type f -name '*.c') | |
OBJECTS:=$(patsubst %,$(BUILD_DIR)/%.o,$(basename $(SOURCE))) | |
DEPS:=$(OBJECTS:.o=.d) | |
BUILD_DIRS:=$(sort $(dir $(OBJECTS))) | |
.PHONY: all clean | |
all: $(NAME) | |
$(NAME): $(OBJECTS) | |
$(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(LIBFLAGS) | |
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIRS) | |
$(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@ | |
$(BUILD_DIRS): | |
mkdir -p $@ | |
include $(wildcard $(DEPS)) | |
clean: | |
rm -rf $(NAME) $(BUILD_DIR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment