-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
93 lines (77 loc) · 2 KB
/
makefile
File metadata and controls
93 lines (77 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Compiler and flags
CC = gcc
CFLAGS = -Wall -Wextra -O2
LDFLAGS = -lm
# Target executable name
TARGET = main
# Automatically find all .c files in current directory
SRCS = $(wildcard *.c)
# Generate object file names from source files
OBJS = $(SRCS:.c=.o)
# Header files (for dependency tracking)
HEADERS = $(wildcard *.h)
# Detect operating system
ifeq ($(OS),Windows_NT)
# Windows
RM = del /Q /F
RMDIR = rmdir /S /Q
EXE_EXT = .exe
MKDIR = mkdir
PATH_SEP = \\
else
# Linux/Unix/macOS
RM = rm -f
RMDIR = rm -rf
EXE_EXT =
MKDIR = mkdir -p
PATH_SEP = /
endif
# Add extension to target
TARGET_EXE = $(TARGET)$(EXE_EXT)
# Default target
all: $(TARGET_EXE)
# Link object files to create executable
$(TARGET_EXE): $(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
@echo Build complete: $(TARGET_EXE)
# Compile .c files to .o files
%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $@
# Clean up generated files
clean:
ifeq ($(OS),Windows_NT)
@if exist *.o $(RM) *.o
@if exist $(TARGET_EXE) $(RM) $(TARGET_EXE)
@echo Clean complete
else
$(RM) $(OBJS) $(TARGET_EXE)
@echo Clean complete
endif
# Clean and rebuild
rebuild: clean all
# Run the program
run: $(TARGET_EXE)
.$(PATH_SEP)$(TARGET_EXE)
# Run with custom input file
run-custom: $(TARGET_EXE)
.$(PATH_SEP)$(TARGET_EXE) $(FILE)
# Show variables (for debugging Makefile)
show:
@echo "Operating System: $(OS)"
@echo "Sources: $(SRCS)"
@echo "Objects: $(OBJS)"
@echo "Headers: $(HEADERS)"
@echo "Target: $(TARGET_EXE)"
@echo "Remove command: $(RM)"
# Phony targets (not actual files)
.PHONY: all clean rebuild run run-custom show help
# Help message
help:
@echo Available targets:
@echo make - Build the project
@echo make clean - Remove generated files
@echo make rebuild - Clean and rebuild
@echo make run - Build and run with default input
@echo make run-custom FILE=myfile.txt - Run with custom input file
@echo make show - Show Makefile variables
@echo make help - Show this help message