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

Major programming languages, the common package tool(s), and the central community package repository.

Most programming languages pair a command-line package tool with a public registry it downloads dependencies from. Examples are cargo with crates.io, pip with PyPI, Maven with Maven…

Read More

MSBuild for Beginners: Project File Fundamentals, Build Flow, and Practical Commands

Learn how MSBuild project files are structured, how targets and tasks execute, and which commands are most useful in CI/CD. This revised guide also preserves the original learning links from the post.

Read More

MSBuild File Extensions Explained: .proj, .targets, .props, .tasks, and *proj Types

Understand what common MSBuild-related file extensions do and how they fit together in real .NET build pipelines. This revised guide includes a comparison table, import-flow diagram, and practical XML examples.

Read More

Host Your Own NuGet Feed: Local Source vs NuGet.Server vs NuGetGallery

Learn how to host a private NuGet feed using three practical approaches: a local folder source, NuGet.Server, and NuGetGallery. This revised guide includes setup steps, command examples, and a workflow diagram to help you choose the right model.

Read More

Publish NuGet Packages to NuGet.org: CLI Steps, Workflow, and CI Example

Learn how to publish a NuGet package to NuGet.org using either nuget.exe or the dotnet CLI. This revised guide includes a workflow diagram, command comparison, and a CI-friendly example.

Read More

How to Create NuGet Packages with nuget.exe (Assembly, .nuspec, and .csproj Methods)

Learn three practical ways to build NuGet packages using nuget.exe: from an existing assembly, from a convention-based folder layout, or directly from a Visual Studio project. This guide includes commands, folder conventions, token behavior, and common pitfalls.

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
1 month 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