Makefile 694 B

1234567891011121314151617181920212223242526272829303132
  1. CC = gcc
  2. CFLAGS = -Wall -Wextra -g
  3. TARGET = cout
  4. # Directories
  5. BUILD_DIR = build
  6. # Source files
  7. SRCS = $(wildcard *.c)
  8. # This transforms "main.c" into "build/main.o"
  9. OBJS = $(patsubst %.c, $(BUILD_DIR)/%.o, $(SRCS))
  10. # Default target
  11. all: $(TARGET)
  12. # Link the object files in the build directory into the final executable
  13. $(TARGET): $(OBJS)
  14. $(CC) $(OBJS) -o $(TARGET)
  15. # Compile .c files into .o files inside the build directory
  16. $(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
  17. $(CC) $(CFLAGS) -c $< -o $@
  18. # Create the build directory if it doesn't exist
  19. $(BUILD_DIR):
  20. mkdir -p $(BUILD_DIR)
  21. # Clean up the build directory and the executable
  22. clean:
  23. rm -rf $(BUILD_DIR) $(TARGET)
  24. .PHONY: all clean