Skip to content

Instantly share code, notes, and snippets.

@ultimateprogramer
Forked from xuhdev/Makefile
Created September 28, 2020 05:57
Show Gist options
  • Select an option

  • Save ultimateprogramer/0209dc082e2b46ed316171aaadb621a5 to your computer and use it in GitHub Desktop.

Select an option

Save ultimateprogramer/0209dc082e2b46ed316171aaadb621a5 to your computer and use it in GitHub Desktop.
# Makefile template for a shared library in C
# https://www.topbug.net/blog/2019/10/28/makefile-template-for-a-shared-library-in-c-with-explanations/
CC = gcc # C compiler
CFLAGS = -fPIC -Wall -Wextra -O2 -g # C flags
LDFLAGS = -shared # linking flags
RM = rm -f # rm command
TARGET_LIB = libtarget.so # target lib
SRCS = main.c src1.c src2.c # source files
OBJS = $(SRCS:.c=.o)
.PHONY: all
all: ${TARGET_LIB}
$(TARGET_LIB): $(OBJS)
$(CC) ${LDFLAGS} -o $@ $^
$(SRCS:.c=.d):%.d:%.c
$(CC) $(CFLAGS) -MM $< >$@
include $(SRCS:.c=.d)
.PHONY: clean
clean:
-${RM} ${TARGET_LIB} ${OBJS} $(SRCS:.c=.d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment