Skip to content
Up To Date Time

Up To Date Time

  • Home
  • Sports
  • cryptocurrency
  • Technology
  • Virtual Reality
  • Education Law
  • More
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
  • Toggle search form
Makefile Template for a Shared Library in C (with Explanations)

Makefile Template for a Shared Library in C (with Explanations)

Posted on July 12, 2025 By rehan.rafique No Comments on Makefile Template for a Shared Library in C (with Explanations)

Last updated on October 28, 2019

tl;dr: Save the following file as Makefile and change the source files to the ones that you intend.

# 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)

The above code snippet is also available on GitHub gist.

Explanation

Basic process: For every C source file (example.c), the C compiler, with the -MM switch, creates a rule file (example.d). The rule file describes the dependencies (e.g., header files) of the object file (example.o) corresponding the C source file. The C compiler then compiles each C source file to an object file. The linker links all object files into the shared library.

  1. Lines 4–8: The compiler command, compiler flags, linker flags, deletion command, and name of the target library, respectively.
  2. Line 10: The list of source files.
  3. Line 11: Object files, inferred from the list of source files by replacing the .c suffix with .o.
  4. Lines 13–14: The all target depends on the target library target. In other words, building the all target, which is the default when running make, will have the target library built.
  5. Line 16: The target library depends on the presence of all object files.
  6. Line 17: Build the target library ($@) by applying the specified compiler command ($(CC)) with the specified linker flags ($(LDFLAGS)) to all object files ($^).
  7. Line 19: There is a rule file (*.d) for every C source file. Its file name is determined by replacing the .c suffix with .d.
  8. Line 20: Create each rule file ($@) by applying to its corresponding C source file ($<) the specified compiler command ($(CC)) with the specified compiler flags ($(CFLAGS)) and the -MM flag.
  9. Line 22: Include the rule files as part of the Makefile.
  10. Lines 24–26: The clean target, which deletes all generated files (${TARGET_LIB}, ${OBJS}, $(SRCS:.c=.d)) using the deletion command (${RM}). This can be invoked by make clean.

This entry was posted in C/C++, GNU on October 28, 2019 by Hong.

Makefile Template for a Shared Library in C (with Explanations)

About Hong

The maintainer of topbug.net.


Technology

Post navigation

Previous Post: Top 8 Crypto Marketing Metrics That Matter for Measuring Success
Next Post: Super Falcons: 7 Greatest Records In WAFCON History

More Related Articles

Secure Your Non-Profit or Place of Worship with Government Security Grants Secure Your Non-Profit or Place of Worship with Government Security Grants Technology
Week in Review: Most popular stories on GeekWire for the week of Nov. 17, 2024 Week in Review: Most popular stories on GeekWire for the week of Nov. 17, 2024 Technology
Learn ML in 21DaysOfCodeWarriors | Learn ML in 21DaysOfCodeWarriors | Technology
Download the Oculus App on PC Windows Download the Oculus App on PC Windows Technology
‘Keep building’: Tariffs cause uncertainty for startups, but VCs encourage a long-term view ‘Keep building’: Tariffs cause uncertainty for startups, but VCs encourage a long-term view Technology
Learning from the Jaguar rebrand campaign Learning from the Jaguar rebrand campaign Technology

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Super Falcons: 7 Greatest Records In WAFCON History
  • Makefile Template for a Shared Library in C (with Explanations)
  • Top 8 Crypto Marketing Metrics That Matter for Measuring Success
  • My thoughts on AI in my XR job – Part 1: Development
  • Elevate Your Visual Experience: The Ultimate Guide to Monitors and VDUs

Categories

  • cryptocurrency
  • Education Law
  • Sports
  • Technology
  • Virtual Reality

Copyright © 2025 Up To Date Time.

Powered by PressBook Blog WordPress theme