Skip to content

Instantly share code, notes, and snippets.

@redxef
Created April 12, 2018 06:05
Show Gist options
  • Save redxef/d9a027f1de70c28bed1645619485030e to your computer and use it in GitHub Desktop.
Save redxef/d9a027f1de70c28bed1645619485030e to your computer and use it in GitHub Desktop.
A makefile for single file libraries in c. The filename is the name of the library and its c source file without any extensions or prefixes.
BUILDTYPE ?= debug
TARGET ?= linux
FILENAME := FILLTHISIN
LIBNAME := lib$(FILENAME).so
CC := clang
CDFLAGS := -g -Wall -Wpedantic -Wextra
CRFLAGS := -Wall -Wpedantic -Wextra -O3
ifeq ($(BUILDTYPE), debug)
CFLAGS := $(CDFLAGS)
else
CFLAGS := $(CRFLAGS)
endif
LFLAGS := -shared $(CFLAGS)
ifeq ($(TARGET), linux)
LIBNAME := lib$(FILENAME).so
endif
ifeq ($(TARGET), macOS)
LIBNAME := lib$(FILENAME).dylib
endif
ifeq ($(TARGET), Windows)
LIBNAME := $(FILENAME).dll
endif
SRC_D := src
OBJ_D := obj
OBJS := obj/$(FILENAME).o obj/test.o
LIBOS := obj/$(FILENAME).o
lib: $(LIBNAME)
clean:
$(RM) -r obj/
$(RM) $(LIBNAME)
$(RM) test
$(OBJ_D)/$(FILENAME).o: $(SRC_D)/$(FILENAME).c $(SRC_D)/$(FILENAME).h
@mkdir -p $(@D)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJ_D)/test.o: $(SRC_D)/test.c $(SRC_D)/$(FILENAME).h
@mkdir -p $(@D)
$(CC) $(CFLAGS) -c $< -o $@
$(LIBNAME): $(LIBOS)
$(CC) $(LFLAGS) -o $@ $<
test: $(OBJS)
$(CC) $(CFLAGS) -Isrc/ -o $@ $(OBJS)
.PHONY: clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment