← All tools

Makefile Generator

Generate a GNU Makefile with build, test, run and clean targets for C, C++, Python, Node.js, Go or Rust.

makefile-generatorLOCAL WORKSPACE

Enabling local controls…

Settings

Use *.c files directly in the source directory with letters, digits, dots, underscores or hyphens in each filename. The test target uses a project test.sh. GNU Make

Results

Makefile

# demo
CFLAGS ?= -O2 -Wall -Wextra
SRC_DIR := src
BUILD_DIR := build
OBJECT_DIR := $(BUILD_DIR)/.objects
TARGET := $(BUILD_DIR)/demo
SOURCES := $(shell export LC_ALL=C; for file in "$(SRC_DIR)"/*.c; do if [ -f "$$file" ]; then case "$$file" in (*[!A-Za-z0-9_./-]*) printf "%s\n" @invalid-source@ ;; (*) printf "%s\n" "$$file" ;; esac; fi; done)
ifneq ($(filter @invalid-source@,$(SOURCES)),)
$(error Source filenames must use letters, digits, dots, underscores and hyphens)
endif
OBJECTS := $(patsubst $(SRC_DIR)/%.c,$(OBJECT_DIR)/%.o,$(SOURCES))
UNSAFE_BUILD_PATH := $(shell for path in "$(BUILD_DIR)" "$(TARGET)" "$(OBJECT_DIR)" $(OBJECTS) $(OBJECTS:.o=.d); do if [ -L "$$path" ]; then printf yes; exit; fi; done; if [ -d "$(SRC_DIR)" ]; then src=$$(CDPATH= cd -P "$(SRC_DIR)" && pwd -P); root=$$(pwd -P); case "$$src/" in ("$$root/$(BUILD_DIR)/"*) printf yes ;; esac; fi)
ifneq ($(UNSAFE_BUILD_PATH),)
$(error Use non-symlink build paths and keep the physical source directory outside build)
endif

all: build
build: $(TARGET)

$(TARGET): $(OBJECTS)
	$(CC) $(LDFLAGS) $^ $(LDLIBS) -o "$@"

$(OBJECT_DIR)/%.o: $(SRC_DIR)/%.c
	@mkdir -p "$(@D)"
	$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c "$<" -o "$@"

run: build
	"./$(TARGET)"

test:
	sh ./test.sh

clean:
	rm -f "$(TARGET)" $(OBJECTS) $(OBJECTS:.o=.d)

-include $(OBJECTS:.o=.d)

.PHONY: all build run test clean
Download Makefile

How to use this tool

Generate a GNU Makefile with build, test, run and clean targets for C, C++, Python, Node.js, Go or Rust.

When not to use it

Uses project-relative paths without spaces. Review the project's toolchain and test commands before using the Makefile.