Skip to content

Instantly share code, notes, and snippets.

@correaswebert
Last active January 28, 2021 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save correaswebert/1603308d601b4be8a20034b3b79895db to your computer and use it in GitHub Desktop.
Save correaswebert/1603308d601b4be8a20034b3b79895db to your computer and use it in GitHub Desktop.
A simple Makefile for C
TARGET := main
SRC := src
INC := include
BIN := bin
TEST := test
BUILD := build
# group the files
HEADER = $(wildcard $(INC)/*.h)
OBJECT = $(patsubst %, $(BUILD)/%, $(notdir $(SOURCE:.c=.o)))
CC = gcc
CFLAGS = -I$(INC) -std=c11
# $@ : target
# $^ : dependency list
# $< : first name in dependency list
# compile target if any object file or header file changes
$(BIN)/$(TARGET): $(OBJECT) $(HEADER)
$(CC) $(CFLAGS) -o $@ $(OBJECT)
# compile object files but do not link them
$(BUILD)/%.o: $(SRC)/%.c
$(CC) $(CFLAGS) -o $@ -c $<
# avoid doing something to file named clean
.PHONY: clean
clean:
rm -f build/* bin/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment