Skip to content

Instantly share code, notes, and snippets.

@urin
Last active October 20, 2023 15:18
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save urin/5971408 to your computer and use it in GitHub Desktop.
Save urin/5971408 to your computer and use it in GitHub Desktop.
makefile - simple template to make an executable from *.cpp files.
COMPILER = g++
CFLAGS = -g -MMD -MP -Wall -Wextra -Winit-self -Wno-missing-field-initializers
ifeq "$(shell getconf LONG_BIT)" "64"
LDFLAGS =
else
LDFLAGS =
endif
LIBS =
INCLUDE = -I./include
TARGET = ./bin/$(shell basename `readlink -f .`)
SRCDIR = ./source
ifeq "$(strip $(SRCDIR))" ""
SRCDIR = .
endif
SOURCES = $(wildcard $(SRCDIR)/*.cpp)
OBJDIR = ./obj
ifeq "$(strip $(OBJDIR))" ""
OBJDIR = .
endif
OBJECTS = $(addprefix $(OBJDIR)/, $(notdir $(SOURCES:.cpp=.o)))
DEPENDS = $(OBJECTS:.o=.d)
$(TARGET): $(OBJECTS) $(LIBS)
$(COMPILER) -o $@ $^ $(LDFLAGS)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
-mkdir -p $(OBJDIR)
$(COMPILER) $(CFLAGS) $(INCLUDE) -o $@ -c $<
all: clean $(TARGET)
clean:
-rm -f $(OBJECTS) $(DEPENDS) $(TARGET)
-include $(DEPENDS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment