Compiling C programs requires you to work with five kinds of files:
.c
by convention. Note: .cc
and .cpp
are C++ files; not C files.e.g., foo.c
.h
by convention.e.g., foo.h
.o
by convention, although on some operating systems (e.g. Windows, MS-DOS), they often end in .obj
.e.g., foo.o
foo.obj
.exe
on Windows.e.g., foo
foo.exe
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..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
.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