The Linker

suggest change

The linker combines object files into an executable or a library. It resolves symbolic references to definitions in other object files or libraries. The result is normally an executable program.

The linker takes the object modules named on the command line, adds system-specific startup code, and resolves external references. Object files may be named directly or pulled in from libraries. For a traditional statically linked executable, it assigns load addresses and rewrites symbolic addresses as numeric addresses. Shared libraries and position-independent executables keep relocations that the dynamic loader applies when the program is loaded.

This includes both the object files that the compiler created from your source code files as well as object files that have been pre-compiled for you and collected into library files. These files have names which end in .a or .so, and you normally don’t need to know about them, as the linker knows where most of them are located and will link them in automatically as needed.

Implicit invocation of the linker

Like the preprocessor, the linker is a separate program, often called ld. GCC invokes it through a driver such as collect2. Also like the preprocessor, the linker is invoked automatically when you use the compiler driver:

% gcc foo.o bar.o baz.o -o myprog

This line tells the compiler to link together three object files (foo.o, bar.o, and baz.o) into a binary executable file named myprog. Now you have a file called myprog that you can run and which will hopefully do something cool and/or useful.

Explicit invocation of the linker

It is possible to invoke the linker directly, but this is seldom advisable, and is typically very platform-specific. That is, options that work on Linux won’t necessarily work on Solaris, AIX, macOS, Windows, and similarly for any other platform. If you work with GCC, you can use gcc -v to see what is executed on your behalf.

Options for the linker

The linker also takes arguments that modify its behavior. The following command tells gcc to link foo.o and bar.o and to include the ncurses library.

% gcc foo.o bar.o -o foo -lncurses

This is actually (more or less) equivalent to

% gcc foo.o bar.o /usr/lib/libncurses.so -o foo

(although libncurses.so could be libncurses.a, which is just an archive created with ar). Note that you should list the libraries (either by pathname or via -lname options) after the object files. With static libraries, the order that they are specified matters; often, with shared libraries, the order doesn’t matter.

On many Unix-like systems, mathematical functions from <math.h> require -lm. macOS does not. POSIX threads, POSIX realtime, and networking are further examples of libraries that are separate on some systems and part of the default C library on others. The linking process varies between platforms.

Other compilation options

This is all you need to know to begin compiling your own C programs. Generally, we also recommend that you use the -Wall command-line option:

% gcc -Wall -c foo.c

The -Wall option causes the compiler to warn you about legal but dubious code constructs, and will help you catch a lot of bugs very early.

If you want the compiler to throw more warnings at you (including variables that are declared but not used, forgetting to return a value etc.), you can use this set of options, as -Wall, despite the name, doesn’t turn all of the possible warnings on:

% gcc -Wall -Wextra -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op \
>     -Wmissing-declarations -Wredundant-decls -Wshadow …

Note that clang has an option -Weverything which really does turn on all warnings in clang.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents