Last active
October 9, 2025 00:16
-
-
Save stvhwrd/668c14a249cd3e46acdc9e07fe301eb3 to your computer and use it in GitHub Desktop.
Sample Makefiles for C language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TARGET = solution | |
LIBS = -lm | |
CC = gcc | |
CFLAGS = -g -Wall | |
.PHONY: default all clean | |
default: $(TARGET) | |
all: default | |
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) | |
HEADERS = $(wildcard *.h) | |
%.o: %.c $(HEADERS) | |
$(CC) $(CFLAGS) -c $< -o $@ | |
.PRECIOUS: $(TARGET) $(OBJECTS) | |
$(TARGET): $(OBJECTS) | |
$(CC) $(OBJECTS) -Wall $(LIBS) -o $@ | |
clean: | |
-rm -f *.o | |
-rm -f $(TARGET) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html | |
# the compiler: gcc for C program, define as g++ for C++ | |
# CC = gcc | |
# compiler flags: | |
# -g adds debugging information to the executable file | |
# -Wall turns on most, but not all, compiler warnings | |
CFLAGS = -g -Wall | |
# the build target executable: | |
TARGET = p1 | |
all: $(TARGET) | |
$(TARGET): $(TARGET).c | |
$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c | |
clean: | |
$(RM) $(TARGET) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Authored by UVic's Amanda Dash | |
# Files in folder: | |
# | |
# . | |
# ├── Makefile | |
# ├── hello.c | |
# └── zombie.c | |
# | |
# 0 directories, 3 files | |
# | |
CC=gcc | |
CFLAGS=-W | |
LIBS=-pthread | |
all: hello zombie | |
debug: CFLAGS += -DDEBUG -g | |
debug: all | |
hello: hello.c | |
$(CC) $(CFLAGS) hello.c -o hello.out $(LIBS) | |
zombie: zombie.c | |
$(CC) $(CFLAGS) zombie.c -o zombie.out $(LIBS) | |
clean: | |
rm -rf *.out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment