====== static linking ====== Static linking, as opposed to shared or dynamic linking, is to bind a library onto an executable. This will make the executable much larger than it would otherwise be, since entire libraries will be stored with the program, even if you only use a small portion of the library in your code. A program that would otherwise be a few kilobytes could grow to several hundred megabytes very quickly, such as a small GTK program that ends up being bundled with entire Gnome libraries. The benefit of static linking is mostly to allow one file to run a program, without requiring the user to have other libraries compiled on their system. You could send a person this one "fat" file, and it would be sure to run on their system. Also, if you have trouble building libraries sometime in the future, you could just load this one file and be sure that the program would run. A drawback to static linking is that you are stuck with the library included at the time the program was built. If there are updates, fixes, or security patches to that library, you would not benefit from them unless you recompile the program and relink against the updated library. Had you linked using shared or dynamic libraries, you would only have to update the library and all programs linked against it would benefit. ===== man page for gcc (Mac OS X) ===== - static ...will not work on Mac OSX unless all libraries have also been compiled with -static ===== example of using a static library ===== The boost regex library for c++ is by default compiled into several versions: * /usr/local/lib/libboost_regex-xgcc40-mt.a (static version) * /usr/local/lib/libboost_regex-xgcc40-mt.dylib (shared version) To compile and link a file against the static version of the library, do this: c++ -I/usr/local/include/boost-1_37/ -o boostregex boostregex.cpp /usr/local/lib/libboost_regex-xgcc40-mt.a Notice how the .a file is just added after the target .cpp file. The result is a single executable (boostregex) which can be used on another system that doesn't have any boost regex library installed.