Skip to content

Instantly share code, notes, and snippets.

@samuelsmal
Last active July 1, 2023 20:55
Show Gist options
  • Save samuelsmal/e43f2001cfc81fee18b6 to your computer and use it in GitHub Desktop.
Save samuelsmal/e43f2001cfc81fee18b6 to your computer and use it in GitHub Desktop.
Generic clang++ (or c++ for that matter) makefile with directory structure
# Requires the following project directory structure:
# /bin
# /obj
# /src
# Use 'make remove' to clean up the hole project
# Name of target file
TARGET = foooooooobar
CXX = clang++
CXXFLAGS = -std=c++11 \
-Weverything -Wall -Wextra -Werror -Wpointer-arith -Wcast-qual \
-Wno-missing-braces -Wempty-body -Wno-error=uninitialized \
-Wno-error=deprecated-declarations \
-pedantic-errors -pedantic \
-Os
LD = clang++ -o
LDFLAGS = -Wall -pedantic
SRCDIR = src
OBJDIR = obj
BINDIR = bin
SOURCES := $(wildcard $(SRCDIR)/*.cpp)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
RM = rm -f
$(BINDIR)/$(TARGET): $(OBJECTS)
@$(LD) $@ $(LDFLAGS) $(OBJECTS)
@echo "Linking complete!"
$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp
@$(CXX) $(CXXFLAGS) -c $< -o $@
@echo "Compiled "$<" successfully!"
.PHONY: clean
clean:
@$(RM) $(OBJECTS)
@echo "Cleanup complete!"
.PHONY: remove
remove: clean
@$(RM) $(BINDIR)/$(TARGET)
@echo "Executable removed!"
@samuelsmal
Copy link
Author

Thanks /u/Leandros99, /u/skoink and /u/dangerbird2.

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