# Preproc options # -MMD is used to generate .d files for user header dependencies (use -MD for system and user header instead) CPPFLAGS = -MMD # main compilation CFLAGS ?= -Werror -Wall -Wextra $(shell pkg-config --cflags sdl) # C++ flags CXXFLAGS = #Linker flags LDFLAGS = #Linker path LDLIBS = $(shell pkg-config --libs sdl) # Force shell to an known one SHELL := bash .SHELLFLAGS := -eu -o -pipefail -c # each recipe in ran as one single shell session (Rather than one new shell per line) .ONESHELL # delete target on error .DELETE_ON_ERROR MAKEFLAGS += --warn-undefined-variables ifneq ($(CROSS_COMPILE),) CC :=$(CROSS_COMPILE)$(CC) CXX :=$(CROSS_COMPILE)$(CXX) LD :=$(CROSS_COMPILE)$(LD) endif bin = main cxxbin = cxxmain lib = libexample.so #Will be compiled with -fpic lib_src = lib.c lib_obj = $(lib_src:%.c=%.o) lib_dep = $(lib_src:%.c=%.d) sources = $(filter-out $(lib_src), $(wildcard *.c)) objects = $(sources:%.c=%.o) depends = $(sources:%.c=%.d) # Do not have same file prefix between C and C++ (e.g. main.c and main.cpp) cxx_sources = $(wildcard *.cpp) cxx_objects = $(cxx_sources:%.cpp=%.o) cxx_depends = $(cxx_sources:%.cpp=%.d) all: $(bin) $(cxxbin) $(lib) $(bin): $(objects) $(cxxbin) : $(cxx_objects) $(lib): CFLAGS += -fpic $(lib): $(lib_obj) # C++ compilation (Use implicit LINK.CC) $(cxxbin): $(LINK.cc) $^ $(LDLIBS) -o $@ %.so: $(LINK.c) -shared $^ $(LDLIBS) -o $@ .PHONY: clean clean: $(RM) $(bin) $(objects) $(depends) $(cxxbin) $(cxx_objects) $(cxx_depends) $(lib) $(lib_obj) $(lib_dep) ifneq ($(MAKECMDGOALS),clean) -include $(depends) -include $(cxx_depends) -include $(lib_dep) endif