Skip to content

Instantly share code, notes, and snippets.

@lixingcong
Last active January 13, 2023 01:51
Show Gist options
  • Save lixingcong/abda5cd77cc82e8ac259023e7ec27993 to your computer and use it in GitHub Desktop.
Save lixingcong/abda5cd77cc82e8ac259023e7ec27993 to your computer and use it in GitHub Desktop.
C/C++ Makefile template
# Get top dir
# https://github.com/imp/dnsmasq/blob/master/Makefile
TOP_DIR != pwd
TOP_DIR ?= $(CURDIR)
BIN_DIR := $(TOP_DIR)/bin
TARGET := $(BIN_DIR)/SimulateServer
OBJ_DIR := $(TOP_DIR)/obj
OBJECTS := $(patsubst %.cpp, %.o, $(wildcard *.cpp))
OBJECTS := $(addprefix $(OBJ_DIR)/, $(OBJECTS))
# Run 'make BUILD_DEBUG=1' to generate gdb debug symbols
ifndef BUILD_DEBUG
override BUILD_DEBUG = 0
endif
ifneq ($(BUILD_DEBUG), 0)
$(info Debug build)
EXTRA_CPPFLAGS := -Wall -W -g
else
$(info Release build)
EXTRA_CPPFLAGS := -Wall -W -O2
endif
all: $(TARGET)
$(TARGET): $(OBJECTS) | $(BIN_DIR)
$(CXX) $(LDFLAGS) $^ -o $(TARGET)
$(OBJECTS): $(OBJ_DIR)/%.o: %.cpp | $(OBJ_DIR)
$(CXX) $(CXXFLAGS) $(EXTRA_CPPFLAGS) $(CPPFLAGS) -c $< -o $@
$(BIN_DIR):
@mkdir -p $@
$(OBJ_DIR):
@mkdir -p $@
.PHONY: clean rebuild
clean:
rm -rf $(BIN_DIR) $(OBJ_DIR)
rebuild:
$(MAKE) clean
$(MAKE) all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment