setup make file to compile .c into /build

This commit is contained in:
2026-06-14 04:01:52 +02:00
parent 840f5f5424
commit a214e58f2c
+32
View File
@@ -0,0 +1,32 @@
CC = gcc
CFLAGS = -Wall -Wextra -g
TARGET = cout
# Directories
BUILD_DIR = build
# Source files
SRCS = $(wildcard *.c)
# This transforms "main.c" into "build/main.o"
OBJS = $(patsubst %.c, $(BUILD_DIR)/%.o, $(SRCS))
# Default target
all: $(TARGET)
# Link the object files in the build directory into the final executable
$(TARGET): $(OBJS)
$(CC) $(OBJS) -o $(TARGET)
# Compile .c files into .o files inside the build directory
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# Create the build directory if it doesn't exist
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
# Clean up the build directory and the executable
clean:
rm -rf $(BUILD_DIR) $(TARGET)
.PHONY: all clean