Skip to content

Instantly share code, notes, and snippets.

@freelsn
Last active April 12, 2023 06:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save freelsn/fe5f160cf564e209dc9c5673296ee813 to your computer and use it in GitHub Desktop.
Save freelsn/fe5f160cf564e209dc9c5673296ee813 to your computer and use it in GitHub Desktop.
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}'
@ArashPartow
Copy link

A comprehensive and easy to use C++ Makefile example can also be found here:

https://www.partow.net/programming/makefile/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment