Skip to content

Instantly share code, notes, and snippets.

@freelsn
Last active Apr 23, 2021
Embed
What would you like to do?
Universal Makefile for C/C++
# - inc/
# - *.h
# - src/
# - *.c
# - *.cpp
# - obj/
# - *.o
# - main
TARGET := main
SOURCES := $(wildcard src/*.c src/*.cpp)
OBJECTS := $(patsubst src%,obj%, $(patsubst %.c,%.o, $(patsubst %.cpp,%.o,$(SOURCES))))
INCLUDE := -I.
LIBPATH :=
LIBS :=
FLAGS := -Wall
CCFLAGS := $(FLAGS) -std=c99
CXXFLAGS := $(FLAGS)
CC := gcc
Cxx := g++
all: $(OBJECTS)
$(CC) $(CCFLAGS) $(INCLUDE) $(OBJECTS) -o $(TARGET) $(LIBPATH) $(LIBS)
%.o: ../src/%.c
$(CC) $(CCFLAGS) $(INCLUDE) -c $< -o $@
%.o: ../src/%.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -o $@
.PHONY: clean help
clean:
rm -rf obj/*
rm -f $(TARGET)
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment