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
.cby convention..ccand.cppare 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
.hby 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
.oby 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
.exeon Windows.
e.g., foo foo.exe
- Libraries: A library is a compiled binary, but it is not itself an executable (it has no
mainfunction). It contains functions that more than one program may use. It should ship with headers that prototype those functions. A source file that uses the library includes the header (for example,#include <library.h>), and the link must name the library so the program can be built. There are two kinds of libraries: static and dynamic.
- Static library: A static library (
.afiles for POSIX systems and.libfiles for Windows — not to be confused with DLL import library files, which also use the.libextension) is copied into the program at link time. Static libraries have the advantage that the program uses a known version of the library. 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 (
.sofiles for most POSIX systems,.dylibfor OSX and.dllfiles 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