Skip to content

Instantly share code, notes, and snippets.

@Xophmeister
Created September 3, 2015 10:07
Show Gist options
  • Save Xophmeister/8e2d4450725b62d13660 to your computer and use it in GitHub Desktop.
Save Xophmeister/8e2d4450725b62d13660 to your computer and use it in GitHub Desktop.
Generic [GNU] Makefile for C projects on OS X (or otherwise), using pkg-config
# Output
TARGET = my_binary
# OS X default is a GCC frontend to LLVM
# Real GCC can be installed via, e.g., Homebrew
CC = gcc
LD = ld
# List of library dependencies
# pkg-config can be installed via, e.g., Homebrew
LIBS = libfoo \
libbar
# Tweak compilation flags (e.g., remove -pthread) as required
# n.b., -Wgnu is LLVM-specific
CFLAGS += -std=gnu11 -Wpedantic -Wall -Wextra -Werror -Wgnu
CFLAGS += -O2 -g
CFLAGS += -pthread
CFLAGS += $(shell pkg-config --cflags $(LIBS))
# Tweak OS X linker flags as required
LDFLAGS += -arch x86_64 -macosx_version_min 10.9.0 -lSystem
LDFLAGS += $(shell pkg-config --libs $(LIBS))
# Every source file should compile into an object,
# which are ultimately all linked into the final binary
SOURCE = $(wildcard *.c)
OBJECTS = $(SOURCE:.c=.o)
$(TARGET): $(OBJECTS)
$(LD) $(LDFLAGS) $+ -o $@
%.o: %.c
$(CC) -c $(CFLAGS) $^
clean:
rm -f $(TARGET)
rm -f $(OBJECTS)
echo:
@echo "TARGET: $(TARGET)"
@echo "SOURCE: $(SOURCE)"
@echo "OBJECTS: $(OBJECTS)"
@echo "CFLAGS: $(CFLAGS)"
@echo "LDFLAGS: $(LDFLAGS)"
.PHONY: clean echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment