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

Cloudflare turns AI against itself with endless maze of irrelevant facts Cloudflare turns AI against itself with endless maze of irrelevant facts Technology
Going Beyond Gigabit: Best multi-Gigabit Routers and Mesh Systems Going Beyond Gigabit: Best multi-Gigabit Routers and Mesh Systems Technology
Cat & the Fiddle, S’porean cheesecake brand owned by Daniel Tay Cat & the Fiddle, S’porean cheesecake brand owned by Daniel Tay Technology
Learning Casting in Java – Top Bug Net Learning Casting in Java – Top Bug Net Technology
Some Child Care Providers Lost All in the Eaton Fire. Why Can’t They Get Relief Money? Some Child Care Providers Lost All in the Eaton Fire. Why Can’t They Get Relief Money? Technology
Paying Migrants to Leave the U.S. – What Lies Beneath?” Paying Migrants to Leave the U.S. – What Lies Beneath?” Technology

Leave a Reply Cancel reply

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

Recent Posts

  • Cashing Out Your NFT Gains
  • Meta’s Reported $800 Smart Glasses with Display Won’t Shoot for the Stars, Claims Respected Analyst
  • Washington Parents: Do you know about ALE?
  • Nepali Youth Team Destructor to Compete at RoboWars 2025
  • Onyedika Confident Abakaliki FC Will Shine In 2025/26 NNL Season

Categories

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

Copyright © 2025 Up To Date Time.

Powered by PressBook Blog WordPress theme