Diagrams That Describe Linking C Programs Roger Kraft Purdue University Calumet ============================================================================================= 1) STATIC LINKING ________ | | my-library.obj my-library.c -->|compiler|----------------+ |________| | /|\ | |(sanity check) |my-library.obj my-library.h--------+ | | | __\|/___ __\|/___ ________ | | client.obj | | client.exe | | executable image client.c --> |compiler|----------->| linker |------------>| loader |-->loaded into |________| |________| |________| virtual memory ============================================================================================= 2) DYNAMIC LINK LIBRARIES my-library.def | ________ __\|/___ | | my-library.obj | | my-library.dll my-library.c -->|compiler|--------------->| linker |---------------+ |________| |________| | /|\ | | |(sanity check) | | my-library.h--------+ |my-library.lib | | | | __\|/___ __\|/___ _\|/__ | | client.obj | | client.exe | | executable image client.c -->|compiler|--------------->| linker |------------|loader|-->loaded into |________| |________| |______| virtual memory NOTE: When you use commercially supplied dll's (like the ones that come with the Windows operating system) you do not see the whole picture given above. The library's source code, obj, and def files need not be supplied by the creators of the library (but they must supply the header, lib, and dll files). In that case, the dynamic linking diagram looks more like this. their-library.h their-library.lib their-library.dll | | | ___\|/__ __\|/___ __\|/___ | | client.obj | | client.exe | | executable image client.c -->|compiler|------------>| linker |------------>| loader |-->loaded into |________| |________| |________| virtual memory NOTE: These diagrams show a simple example where there is just one library that needs to be linked to the client program. In practice, any number of library modules can be used to produce the final running process. In the case of static linking, all of the library obj modules would be linked with the client obj file at the one linker step described above. In the case of dynamic linking, each of the library modules would be linked in separate steps (to produce each dll), then all of the lib files would be linked to the client obj in one step, and then the client exe would be loaded with all of the dll's in virtual memory at runtime (draw yourself a picture of this in the case of three or four library modules). It is also possible to mix static linking with dynamic linking. Try drawing a schematic of linking a client program with three static libraries and two dynamic libraries. ============================================================================================= 3) STATIC LINK LIBRARIES The above diagrams show how "dynamic link libraries" (dll's) are created and used. There is also the concept of "static link libraries" that generalize the first diagram at the top of this page. A "static link library" is a collection of obj modules combined (not linked) into a single lib file. This combining is done by a "librarian program" named lib.exe. The following diagram shows how a static link library (made up of n obj modules) might be created and used. ________ | |library_1.obj library_1.c ->|compiler|--------------+ |________| | ________ | ___________ | |library_2.obj +->| | library_2.c ->|compiler|---------------->|lib program| . |________| +->|___________| . ________ | | . | |library_n.obj | | library_n.c ->|compiler|--------------+ | |________| | |my-library.lib | my-library.h | | | __\|/___ ___\|/__ ______ | | client.obj | | client.exe | | executable image client.c -->|compiler|----------->| linker |------------>|loader|-->loaded into |________| |________| |______| virtual memory NOTE: When you use commercially supplied libraries (like the ones that come with the Windows operating system) you do not see the whole picture given above. The library's source code and obj files need not be supplied by the creators of the library (but they must supply the header and lib files). In that case, the linking diagram looks more like this. their-library.h their-library.lib | | __\|/___ ___\|/__ ______ | | client.obj | | client.exe | | executable image client.c -->|compiler|----------->| linker |------------>|loader|-->loaded into |________| |________| |______| virtual memory NOTE: The "my-library.h" and "their-library.h" headers may in fact each be a number of headers. There could be one header for each of the obj modules in the lib file. This is in fact the way the C Library works. There is one lib file for the whole library (see below) and a large number of header files (the standard C headers). ============================================================================================ NOTE: Notice how lib files for dynamic link libraries and lib files for static link libraries play very similar roles in the linking process but they are very different kinds of objects produced by different kinds of programs (a linker vs. a librarian program). NOTE: Many commercially available libraries come in both a static and a dynamic version. Such a library needs to come with two lib files. They will usually have different names. For example, in Microsoft's Visual Studio 6, the file C:\Program Files\Microsoft Visual Studio\VC98\Lib\libc.lib is the lib file for the static link library version of the C Library, and C:\Program Files\Microsoft Visual Studio\VC98\Lib\msvcrt.lib is the lib file for the dynamic link library version of the C Library. You tell the cl.exe compiler driver which one of these lib files to use by using either the \ML or \MD options on the compiler's command line. (There are also "debug" versions of these libraries with lib files libcd.lib and msvcrtd.lib and command line options \MLd and \MDd.) NOTE: One last issue that you should think about is where are the header, lib, and dll files kept and how do the compiler, linker, and loader find them?