YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Building programsChapter 43 of 73

Linking and libraries

Follow object files into an executable, build a small static archive, and distinguish language guarantees from toolchain conventions.

5 min readIntermediateC17 baseline
Offline reading
In this chapter8 sections
0% through this chapter

Translation can succeed for separate files while the final program still fails to link. Linking connects required definitions with their uses. Libraries package reusable work, but compatible names, types, and platform conventions still matter.

1. Object files are translated pieces, not C objects

A conventional object file contains translated code, data, symbol information, and relocation information used by later tools. It is different from an object in C’s language model, such as a live int. The standard describes translation and resolution requirements without requiring a particular .o format, an independently installed linker, or a particular machine instruction set.

Two jobs in a conventional toolchain
Compile source files
Check visible declarations; produce object files
Link selected pieces
Connect references with implementations
Optimizations such as link-time optimization can cross these conventional boundaries.

2. Follow one interface through three files

This operation chooses the larger of two unsigned values. It performs a comparison rather than arithmetic that could overflow. The header supplies the declaration; the implementation supplies the body; the caller supplies values and checks the result. Save the files under the names shown.

C17 · project file · choice.h
#ifndef CODEDILE_CHOICE_H
#define CODEDILE_CHOICE_H

unsigned codedile_max(unsigned first, unsigned second);

#endif
C17 · project file · choice.c
#include "choice.h"

unsigned codedile_max(unsigned first, unsigned second)
{
    return first > second ? first : second;
}
C17 · project file · main.c
#include "choice.h"
#include <assert.h>
#include <stdio.h>

int main(void)
{
    unsigned larger = codedile_max(3, 7);
    assert(larger == 7);
    assert(codedile_max(4, 4) == 4);
    printf("larger=%u\n", larger);
    return 0;
}

3. Compile separately, then link

These are example GCC commands for a Unix-like shell in the folder containing the three files. They are tool commands, not C statements. -c stops before linking. In the final command, using the GCC driver also arranges the normal target startup and library support; invoking a raw linker directly requires more platform knowledge.

GCC / GNU tools · example commands
gcc -std=c17 -Wall -Wextra -c choice.c -o choice.o
gcc -std=c17 -Wall -Wextra -c main.c -o main.o
gcc main.o choice.o -o choice_demo
./choice_demo

The program prints larger=7. If you omit choice.o from the link command, the declaration in choice.h still lets main.c compile, but the checked build reports a missing definition at link time. Including the header again cannot supply that missing body. Add the correct implementation object or library to the build.

4. A static archive packages object members

A static library on a common GNU toolchain is an archive of object members. During a conventional link, the linker selects needed members and incorporates their contents into the output. It does not simply copy the whole archive file into the executable. Special options, dependencies between members, and later optimizations can affect the selected contents.

GCC / GNU tools · example commands
ar rcs libchoice.a choice.o
gcc main.o -L. -lchoice -o choice_archive
./choice_archive

Only the archive candidate is present in this example; if a same-named shared library is also present, the search may prefer it. Naming ./libchoice.a explicitly selects this archive. Here GNU ar rcs creates or updates libchoice.a and its symbol index. GCC’s -L. adds the current folder to the library search path; -lchoice requests a platform-appropriate library named choice. With a conventional GNU archive search, put the archive after the objects that reference it: main.o -lchoice. Reversing that order can leave references unresolved. These naming and search rules are toolchain conventions.

5. Shared libraries add a deployment relationship

A shared library usually remains a separate binary dependency. Binding can occur during linking, program loading, or later execution, depending on the platform and options. Typical suffixes include .so, .dylib, and .dll; none is a standard C guarantee. A program may combine static and shared dependencies. A statically linked component also does not imply that every system component is embedded.

An installed header and an installed library solve different problems. A compatible binary also needs the right target architecture, calling conventions, data layout, and library version: part of the environment’s ABI. A same-spelled symbol does not prove that two function types agree. Rebuild dependent code when an interface or ABI changes, and follow the library’s documented ownership and error rules.

Primary references and further reading

Before you move on

  • Linking connects references with the definitions supplied to a build.
  • Static archives and shared libraries package components under toolchain-specific rules.
  • A compatible name alone does not establish the full ABI and ownership contract.
Keep these ideas separate
Library available / Library correctly used
A found library still needs compatible interfaces, suitable link options, and the required runtime environment.
Explain it in your own words

Which evidence would separate a missing definition from an incompatible declaration or unavailable shared dependency?

Optional: say it aloud or keep a note. You can continue whenever you are ready.

Check your understanding

Optional. Answers are not saved or sent, and reading stays unrestricted.

What is the difference between seeing a library declaration and supplying its definition?
Read the explanation for every choice
  1. The declaration supports type checking; the build still needs the required implementation.

    ✓ Correct reasoning. How a toolchain supplies library definitions is separate from header inclusion.

  2. Including a header always builds every library on the computer.

    Header inclusion supplies source declarations, not a universal library-build operation.

  3. Every C implementation requires the same library command-line flags.

    Toolchain flags are implementation-specific interfaces.

C17

Ready for the next idea?

Mark this chapter as read whenever you feel ready.

CHAPTER 43 / 73

In this chapter

Linking and libraries

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping