During my phases where I learn to write in C, I don’t want to keep creating new Makefiles. Thus after searching all over the net I managed to put together this simple Makefile. It expects your source code to be in ./src and it will put the compiled executables in ./bin
# # Makefile to compile arbitrary C sources to an executable with the same name # The source files are in the src/ sub directory # The compiled executables are in the bin/ sub directory # # $@ is automatic variable that holds name of target # $< is automatic variable that holds the name of the prerequisite # defines the sub directory in which the sources are found SRC_PATH=./src/ # defines the sub directory in which the executables are compiled to BIN_PATH=./bin/ # tells make to search in $(SRC_PATH) for the .c files VPATH = src:$(SRC_PATH) # defines the name of the executables which are to be compiled EXECUTABLES= $(patsubst $(SRC_PATH)%.c,%,$(shell ls $(SRC_PATH)*.c)) # default target which prepares the bin/ sub directory, and initiates the compilation all: prepare $(EXECUTABLES) # the target which is expanded to compile each found C source file $(EXECUTABLES): %: %.c gcc $< -lm -o $(BIN_PATH)$@ # declare phony targets .PHONY: prepare clean # create the $(BIN_PATH) sub directory prepare: mkdir -p $(BIN_PATH) # remote the $(BIN_PATH) subdirectory clean: rm -rf $(BIN_PATH)