| name | makefile-generation |
| description | Creating Makefiles for build automation. Use when writing build rules, dependencies, or compilation workflows. |
Makefile Generation
GNU Make build configuration.
Quick Start
CC = gcc
CFLAGS = -Wall -O2
TARGET = myapp
SRCS = main.c utils.c
OBJS = $(SRCS:.c=.o)
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -f $(TARGET) $(OBJS)
.PHONY: clean
Key Patterns
# Variables
BUILD_DIR ?= build
PREFIX ?= /usr/local
# Automatic variables
# $@ - target name
# $< - first prerequisite
# $^ - all prerequisites
# Conditional
ifdef DEBUG
CFLAGS += -g -DDEBUG
endif
# Platform detection
UNAME := $(shell uname)
ifeq ($(UNAME), Linux)
LDFLAGS += -lpthread
endif
# Install target
install: $(TARGET)
install -d $(DESTDIR)$(PREFIX)/bin
install -m 755 $(TARGET) $(DESTDIR)$(PREFIX)/bin