File Types
suggest changeCompiling C programs requires you to work with five kinds of files:
- Source files: These files contain function definitions, and have names which end in
.c
by convention. Note:.cc
and.cpp
are C++ files; not C files.
e.g., foo.c
- Header files: These files contain function prototypes and various pre-processor statements (see below). They are used to allow source code files to access externally-defined functions. Header files end in
.h
by convention.
e.g., foo.h
- Object files: These files are produced as the output of the compiler. They consist of function definitions in binary form, but they are not executable by themselves. Object files end in
.o
by convention, although on some operating systems (e.g. Windows, MS-DOS), they often end in.obj
.
e.g., foo.o
foo.obj
- Binary executables: These are produced as the output of a program called a “linker”. The linker links together a number of object files to produce a binary file which can be directly executed. Binary executables have no special suffix on Unix operating systems, although they generally end in
.exe
on Windows.
e.g., foo
foo.exe
- Libraries: A library is a compiled binary but is not in itself an an executable (i.e., there is no
main()
function in a library). A library contains functions that may be used by more than one program. A library should ship with header files which contain prototypes for all functions in the library; these header files should be referenced (e.g;#include <library.h>
) in any source file that uses the library. The linker then needs to be referred to the library so the program can successfully compiled. There are two types of libraries: static and dynamic.
- Static library: A static library (
.a
files for POSIX systems and.lib
files for Windows — not to be confused with DLL import library files, which also use the.lib
extension) is statically built into the program . Static libraries have the advantage that the program knows exactly which version of a library is used. On the other hand, the sizes of executables are bigger as all used library functions are included.
e.g., libfoo.a
foo.lib
- Dynamic library: A dynamic library (
.so
files for most POSIX systems,.dylib
for OSX and.dll
files for Windows) is dynamically linked at runtime by the program. These are also sometimes referred to as shared libraries because one library image can be shared by many programs. Dynamic libraries have the advantage of taking up less disk space if more than one application is using the library. Also, they allow library updates (bug fixes) without having to rebuild executables.
e.g., foo.so
foo.dylib
foo.dll
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents