Linking and libraries
Follow object files into an executable, build a small static archive, and distinguish language guarantees from toolchain conventions.
In this chapter8 sections
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.
- Compile source files
- Check visible declarations; produce object files
- Link selected pieces
- Connect references with implementations
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.
#ifndef CODEDILE_CHOICE_H
#define CODEDILE_CHOICE_H
unsigned codedile_max(unsigned first, unsigned second);
#endif
#include "choice.h"
unsigned codedile_max(unsigned first, unsigned second)
{
return first > second ? first : second;
}
#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 -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.
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
- WG14 N1570 — public C11 committee draft: 5.1.1, 6.2.7, 6.9. Stable core rules used for the C17 baseline; not the final ISO publication.
- GCC: overall options
- GCC: link options
- GNU ar: operation and modifiers
- GNU ld: archive search
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.
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.
Read the explanation for every choice
- 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.
- Including a header always builds every library on the computer.
Header inclusion supplies source declarations, not a universal library-build operation.
- 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.