Makefile Explained - alteixeira20/42_minishell GitHub Wiki


This page documents the structure, logic, and utilities of the MiniShell Makefile.

🔧 Core Variables — File names, paths, flags

Executables & Binaries:

  • NAME — Output binary (minishell)
  • LIBFT_REPO — Remote git repo to clone libft if not present

Project Paths:

  • SRC_DIR — Root source code directory
  • LIBFT_DIR — Cloned libft folder
  • OBJ_DIR — Directory where .o files are stored

Compiler Flags:

  • CFLAGS-Wall -Wextra -Werror for strict compiling
  • DFLAGS-g for debugging support (Valgrind)
🧱 Object Rules — How source becomes `.o` files

Automatic Compilation:

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	@mkdir -p $(@D)
	@$(CC) $(CFLAGS) $(DFLAGS) -c $< -o $@
  • Ensures matching folders are created for each object file
  • Applies CFLAGS and DFLAGS per .c source
🛠️ Build Targets — Main and helper recipes

all / default:

  • Runs make on libft if missing
  • Builds minishell binary

$(NAME):

  • Links all compiled objects + libft + readline
  • Outputs final executable

$(LIBFT):

  • Clones libft if not already present
  • Compiles silently using make -C
🧽 Clean Targets — Removing build artifacts

make clean:

  • Deletes object files and executable only if they exist

make fclean:

  • Runs clean
  • Also removes libft/ if it was cloned

Smart Printouts:
Each clean target only prints messages if work was actually done.

📏 Norminette — Code style checks

make norm:

  • Runs Norminette on every source file
  • Uses color-coded output:
    • ✅ for compliant files
    • ❌ for files with errors
🧠 Valgrind — Memory leak checking

make valgrind:

  • Checks if minishell exists, builds it if needed
  • Creates readline.supp suppression file on the fly
  • Runs with --leak-check=full, suppressing false positives from readline and add_history
  • Cleans up suppression file automatically
❓ Help Target — Command reference

make help:

  • Prints available Makefile targets
  • Parses inline comments using awk
##@ Help Section
help:        ## Display this help page
🎨 Color System — Terminal output enhancements
  • Uses tput to generate ANSI escape codes
  • Makes messages more readable (green success, red errors, blue info)

Preview:

$(shell tput setaf 2)   # green
$(shell tput setaf 1)   # red
$(shell tput bold)      # bold
$(shell tput sgr0)      # reset

⚠️ **GitHub.com Fallback** ⚠️