-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeFile
More file actions
62 lines (49 loc) · 2.78 KB
/
MakeFile
File metadata and controls
62 lines (49 loc) · 2.78 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
# Compiler and flags
CC = gcc # Set the compiler to GCC
CFLAGS = -Wall -Wextra -std=c11 # Compiler flags: show all warnings, use C11 standard
LDFLAGS = # Linker flags (none defined for now)
# Directories
SRCDIR = ./src # Source code directory
BUILDDIR = ./build # Build (object files and executables) directory
TESTDIR = ./tests # Test files directory
# Source and Object files
SRC = $(wildcard $(SRCDIR)/*.c) # Find all .c files in src/
OBJ = $(patsubst $(SRCDIR)/%.c,$(BUILDDIR)/%.o,$(SRC)) # Map .c files to .o files in build/
# Executables
MAIN_EXEC = $(BUILDDIR)/main # Main executable
TEST_EXEC = $(BUILDDIR)/test_memalloc # Test executable
# OS Detection and Platform-Specific Settings
ifeq ($(OS),Windows_NT) # If running on Windows
SHELL = pwsh # Use PowerShell 7 as the shell
RM = Remove-Item -Force -Recurse # PowerShell command to delete files
MKDIR = New-Item -ItemType Directory -Force # PowerShell command to create directories
RUN_CMD = $(MAIN_EXEC).exe # Command to run the main executable
else # Assume a UNIX-like system
SHELL = /bin/sh # Use the default shell for Linux/Mac
RM = rm -rf # Command to delete files
MKDIR = mkdir -p $(BUILDDIR) # Command to create build directory
RUN_CMD = ./$(MAIN_EXEC) # Command to run the main executable
endif
# Default target
all: main # Default target is to build the main executable
# Compile main executable
main: $(MAIN_EXEC) # 'main' depends on the main executable
$(MAIN_EXEC): $(OBJ) # Main executable depends on object files
$(CC) $(CFLAGS) -o $@ $(SRCDIR)/main.c $(OBJ) # Compile and link
# Compile test executable
test: $(TEST_EXEC) # 'test' depends on the test executable
$(TEST_EXEC) # Run the test executable
$(TEST_EXEC): $(OBJ) # Test executable depends on object files
$(CC) $(CFLAGS) -o $@ $(TESTDIR)/test_memalloc.c $(OBJ) # Compile and link
# Build object files from source files
$(BUILDDIR)/%.o: $(SRCDIR)/%.c # Rule to compile .c files to .o files
@$(MKDIR) $(BUILDDIR) # Create build directory if it doesn't exist
$(CC) $(CFLAGS) -c $< -o $@ # Compile source file into object file
# Clean target to remove build artifacts
clean:
$(RM) $(BUILDDIR)/* # Remove all files in build directory
# Run the main executable
run: main
$(RUN_CMD) # Run the main executable
# Phony targets to avoid conflicts with files named 'all', 'main', 'test', 'clean', etc.
.PHONY: all main test clean run