Last active
November 21, 2024 19:07
-
-
Save freelsn/fe5f160cf564e209dc9c5673296ee813 to your computer and use it in GitHub Desktop.
Universal Makefile for C/C++
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
# - 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
A comprehensive and easy to use C++ Makefile example can also be found here:
https://www.partow.net/programming/makefile/index.html