Changelog:

  • 17 Jan 2024: merge text previously in glossary under makefiles into here; mention explicitly that standard library is linked by default

1 (review?) clang/gcc modes

The clang program (or the gcc or cc programs, which work essentially the same way) can perform assembly, linking, and compilation, depending on how it is invoked:

Or you can perform multiple at once:

You can specify a different output file for any of these commands with -o FILENAME, for example:

2 selected other clang/gcc options

3 producing and using libraries

A library is a collection of related implementations. For C or C++, a libary is generally provided as a library file and (one or more) header files.

There are two types of library files:

3.1 producing and using static libraries (on Unix-like systems)

Static libraries on most Unix-like systems are archives of object files produced with the ar command. After generating object files,

ar -rcs libfoo.a quux.o bar.o

will create a library libfoo.a.2

Then, if the resulting libfoo.a is placed in one of the standard system directories3 a command like

clang -o exec main.o -lfoo

will create an executable linking main.o and the library together. You should usually put the -lXXX flags last on the command line: the linker processes the files listed in order and, when adding a static library, by default only includes parts that appear to be needed by files it has already processed.

If libfoo.a is not placed in one the standard system directory, then add -L:

clang -o exec main.o -LdirectoryContainingLibFoo -lfoo

3.2 producing and using dynamic libraries (on Linux)

On Linux, files that will be used in a dynamic library need to be compiled specially so they can be loaded anywhere in memory. This can be done by using the -fPIC option when compilng.

Then, you can generate a library by linking with -shared as in:

clang -shared -o libfoo.so bar.o quux.o

Then, like static libraries, if the resulting libfoo.so is placed in one of the standard system directories4 a command like

clang -o exec main.o -lfoo

will create an executable exec that loads libfoo.so at runtime.

If libfoo.so is not placed in one these directories, then you will need to:

4 Glossary

Header File

A file (.h for C; .h, .hpp, .H or .hh for C++) that provides

  • the signatures of functions, but not their definitions
  • the names and types of global variables, but not where in memory they go
  • typedefs and #defines
Object File
A file (.o on most systems, .obj on Windows) that contains assembled binary code in the target ISA, with metadata to allow the linker to place it in various locations in memory depending on what other files it is linked with.
Standard Library

A library to which every program is linked by default. Virtually every language has a standard library, which does much to define the character of the language.

The C language’s standard library is typically called libc and is defined by the C standard (see https://en.wikipedia.org/wiki/C_standard_library); there are several implementations of it, but glibc is probably the most pervasive on Linux.

Static Library
A file (.a on most Unix-like systems, .lib on Windows) that contains one more more object files together with metadata needed to connect them into a running code system by the linker. This compile-time linkage means that each executable has its own copy of the library stored internally to the file, without external dependencies.
Source File

A file (.c for C, .cpp, .C, or .cc for C++) that provides

  • the implementation of functions
  • the declaration and implementation of provided helper functions
  • the memory in which to store global variables

5 Additional resources

On Clang and GCC options:

On linking and libraries:


  1. This is the default name on Unix-like systems; on native Windows, it may be a.exe.↩︎

  2. The c option says to create a new archive silently if one does not exist; the r option to add to or update the archive; and the s option to create an index of the object files for the linker to use. Sometimes you might see the index creation done by a separate ranlib command.↩︎

  3. clang -print-search-dirs lists these↩︎

  4. clang -print-search-dirs lists the directories that are searched when the executable is geneated. For loading libraries when an executable runs, the system searches /lib, /usr/lib, and directories specified by the configuration file /etc/ld.so.conf.↩︎