Last active
September 13, 2022 18:09
-
-
Save vanaur/700130fe6952ba6a34ef8ebf64ef6d3d to your computer and use it in GitHub Desktop.
Generic makefile for C projects that follow the "app" + "include" + "source" configuration.
This file contains 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
# Defines which shell command to use to clean the terminal, depending on the platform. | |
ifeq ($(OS), Windows_NT) | |
# Clean on Windows | |
clean_cmd = @del /s /q *.o > null | |
else | |
# Clean on UNIX systems | |
clean_cmd = @rm -f *.o | |
endif | |
# A recursive function that allows to list all files with a given extension, in folders and sub-folders, from a given source. | |
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)) | |
# Name of the generated program | |
target := <program name> | |
# Arguments to be passed to the compiler. | |
copt_G := -g -Wall # For debug | |
copt_O := -O3 # For release | |
cinclude := include | |
cwarns := -Wno-unused-function | |
cflags := $(copt_G) -I$(cinclude) $(cwarns) | |
export CFLAGS = $(cflags) | |
# Root and directories | |
srcroot := source/ | |
approot := app/ | |
objroot := ./obj | |
# Files of library | |
lib_src := $(call rwildcard,$(srcroot),*.c) | |
lib_obj := $(patsubst %.c,%.o,$(lib_src)) | |
# Files of application | |
app_src := $(call rwildcard,$(approot),*.c) | |
app_obj := $(patsubst %.c,%.o,$(app_src)) | |
$(target): $(lib_obj) $(app_obj) | |
$(CC) -o $@ $^ | |
$(info Compilation completed!) | |
$(objroot)/%.o: %.c | |
$(CC) -c $< | |
clean: | |
$(clean_cmd) | |
all: $(target) | |
.PHONY: all | |
.SILENT: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment