EX_03/level-2/n_queens/Makefile
2025-09-23 08:41:21 +01:00

74 lines
1.7 KiB
Makefile

# Program name
NAME = n_queens
# Compiler and flags
CC = gcc
CFLAGS = -Wall -Wextra -Werror
# Source files
SRCS = n_queens.c
# Object files
OBJS = $(SRCS:.c=.o)
# Header files
HEADERS = n_queens.h
# Colors for output
GREEN = \033[0;32m
RED = \033[0;31m
RESET = \033[0m
# Main rule
all: $(NAME)
# Compile the program
$(NAME): $(OBJS)
@echo "$(GREEN)Compiling $(NAME)...$(RESET)"
@$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
@echo "$(GREEN)$(NAME) compiled successfully!$(RESET)"
# Compile object files
%.o: %.c $(HEADERS)
@echo "Compiling $<..."
@$(CC) $(CFLAGS) -c $< -o $@
# Clean object files
clean:
@echo "$(RED)Cleaning object files...$(RESET)"
@rm -f $(OBJS)
@echo "$(RED)✓ Object files cleaned!$(RESET)"
# Clean everything
fclean: clean
@echo "$(RED)Cleaning executable...$(RESET)"
@rm -f $(NAME)
@echo "$(RED)✓ Everything cleaned!$(RESET)"
# Rebuild everything
re: fclean all
# Test the program with different values
test: $(NAME)
@echo "$(GREEN)Testing n_queens program:$(RESET)"
@echo "\n$(GREEN)Testing with n=1:$(RESET)"
@./$(NAME) 1
@echo "\n$(GREEN)Testing with n=2 (no solutions):$(RESET)"
@./$(NAME) 2 || echo "No solutions found"
@echo "\n$(GREEN)Testing with n=4:$(RESET)"
@./$(NAME) 4
@echo "\n$(GREEN)Testing with n=8 (counting solutions):$(RESET)"
@echo "Number of solutions for n=8: $$(./$(NAME) 8 | wc -l)"
# Display help
help:
@echo "$(GREEN)Available commands:$(RESET)"
@echo " make - Compile the program"
@echo " make clean - Remove object files"
@echo " make fclean - Remove all generated files"
@echo " make re - Rebuild everything"
@echo " make test - Run tests with different values"
@echo " make help - Show this help message"
# Declare phony targets
.PHONY: all clean fclean re test help