MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Makefile Guide for C Libraries: Build Static and Shared Targets Cleanly

This guide explains a classic non-recursive Makefile for a C project that builds both a static library (.a) and a shared library (.so). You will learn what each target does, how make executes the workflow, and how to modernize the file while keeping its original intent.

Originally published on 2009-01-29; fully revised and updated.

makefile example from original post

What the original Makefile is doing

The original post shows an older, single-file build style: source files are listed directly, object files are derived from that list, and two library targets are built from the same objects. This is still a valid approach for small C projects.

CC = gcc
CFLAGS = -Wall -O2
LFLAGS +=

SRCS = cas.c \
       client.c \
       hashtable_itr.c
OBJS = $(SRCS:.c=.o)

all: libmarquis.a libmarquis.so

clean:
\trm -f libmarquis.a libmarquis.so *.o

libmarquis.a: $(OBJS)
\tar -r $@ $(OBJS)

libmarquis.so: $(OBJS)
\t$(CC) -shared $(CFLAGS) -fPIC -Wl,-soname,libmarquis.so -o $@ $(OBJS)

.c.o:
\t$(CC) $(CFLAGS) -c $<Code language: JavaScript (javascript)

Important: recipe lines in a Makefile must begin with a real tab character. If tabs are converted to spaces, builds fail with a “missing separator” error.

Build workflow at a glance

flowchart TD
    A[Run make all] --> B[Compile each C file into object file]
    B --> C[Archive static library libmarquis.a]
    B --> D[Link shared library libmarquis.so]
    C --> E[Artifacts ready]
    D --> E

Static vs shared library targets

TargetOutputCommand styleTypical use
libmarquis.aStatic archivear -r ...Link code into binaries at build time
libmarquis.soShared objectgcc -shared ...Load library dynamically at runtime

Useful commands while developing

# Build both outputs (default target)
make

# Build only the static library
make libmarquis.a

# Build only the shared library
make libmarquis.so

# Show commands without executing them
make -n

# Force rebuild of targets
make -B

# Remove generated artifacts
make cleanCode language: PHP (php)

Modernized template with the same goals

The following version keeps the same intent, but uses a pattern rule, marks phony targets, and tracks header dependencies automatically.

CC ?= gcc
AR ?= ar
CFLAGS ?= -Wall -O2
CPPFLAGS ?=
LDFLAGS ?=

SRCS := cas.c client.c hashtable_itr.c
OBJS := $(SRCS:.c=.o)
DEPS := $(OBJS:.o=.d)

.PHONY: all clean
all: libmarquis.a libmarquis.so

libmarquis.a: $(OBJS)
\t$(AR) rcs $@ $(OBJS)

libmarquis.so: $(OBJS)
\t$(CC) -shared -Wl,-soname,libmarquis.so -o $@ $(OBJS) $(LDFLAGS)

%.o: %.c
\t$(CC) $(CPPFLAGS) $(CFLAGS) -fPIC -MMD -MP -c $< -o $@

clean:
\trm -f $(OBJS) $(DEPS) libmarquis.a libmarquis.so

-include $(DEPS)Code language: JavaScript (javascript)

Best practices

  • Keep target names predictable (all, clean, and output names).
  • Use .PHONY for non-file targets to avoid accidental conflicts.
  • Prefer pattern rules (%.o: %.c) over older suffix rules when maintaining or extending builds.
  • Generate and include dependency files so header changes trigger correct rebuilds.
  • Treat linker options (like -Wl,-soname,...) as platform-specific and validate them in your CI environment.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Splunk Observability Cloud vs Splunk AppDynamics vs Splunk Cloud Platform

Here’s a clear and updated comparison between Splunk Observability Cloud, Splunk AppDynamics, and Splunk Cloud Platform, highlighting their purpose, strengths, and ideal use cases: 📦 Product Overview…

Read More

Top 10 Virtualization Software Tools in 2026: Features, Pros, Cons & Comparison

Introduction Virtualization software is one of the foundational technologies driving the IT landscape in 2026. With the growing demand for cloud computing, IT professionals and businesses are…

Read More

Top 10 Patch Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction: The Importance of Patch Management in 2026 In 2026, as cyber threats evolve and technology becomes more complex, patch management tools are critical for maintaining cybersecurity…

Read More

Top 10 Remote Monitoring & Management (RMM) Tools in 2026: Features, Pros, Cons & Comparison

Introduction In today’s fast-paced, technology-driven world, businesses rely on the seamless functioning of their IT infrastructure. This makes Remote Monitoring & Management (RMM) tools essential for managing,…

Read More

Top 10 Server Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction Server management tools are essential for businesses in 2026 to maintain the performance, reliability, and security of their IT infrastructure. These tools help streamline the management…

Read More

Top 10 Endpoint Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, businesses of all sizes are increasingly reliant on a variety of devices—laptops, desktops, mobile devices, and other endpoints—that connect to their networks. With the…

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
14 days ago

A well-organized Makefile can save time by making common build tasks easy to repeat. Keeping each target focused on one job helps developers understand and maintain the build process. It is also important to define dependencies correctly so changes trigger the right rebuilds. Use clear target names and simple comments to help new team members get started. Testing the build from a clean state can catch missing steps before they cause problems.

1
0
Would love your thoughts, please comment.x
()
x