The CL command uses the following procedure to create an executable file from one or more C source files:
1.CL compiles each source file, creating an object file for each. CL automatically includes the options listed in the CL environment variable. In each object file, CL places the name of the appropriate standard combined library. The library's name reflects both the memory model and the floating-point math package used to compile the program. See “/A Options” for more information on the library names.
2.CL invokes the linker, passing the names of the object files it has created plus the name of any object files or libraries given on the command line. CL automatically includes the options listed in the LINK environment variable. If you use /link to specify linker options on the CL command line, these options apply as well. If conflicts occur, options that follow /link override those in the LINK environment variable.
3.The linker links the object files and libraries named by CL to create a single executable file.
Before it creates the executable file, the linker resolves “external references”
in the object files. An external reference is a function call in one object file that refers to a function defined in another object file or in a library. To resolve an external reference, the linker searches for the called function in the following locations in the following order:
A.The object files passed by CL
B.The libraries given on the CL command line, if any
C.The libraries named in the object files
Example
Assume that you are compiling three C source files: MAIN.C, MOD1.C, and MOD2.C. Each file includes a call to a function defined in a different file:
MAIN.C calls the function named func1 in MOD1.C and the function named func2 in MOD2.C.
MOD1.C calls the standard library functions printf and scanf.
MOD2.C calls graphics functions named myline and mycircle, which are defined in a library named MYGRAPH.LIB.
First, compile with a command line of the following form:
CL MAIN.C MOD1.C MOD2.C MYGRAPH.LIB
CL first compiles the C source files and creates the object files MAIN.OBJ, MOD1.OBJ, and MOD2.OBJ. The compiler places the name of the standard library SLIBCE.LIB in each object file.
Next, CL passes the names of the C source files to the linker. Finally, the linker
resolves the external references as follows:
1.In MAIN.OBJ, the reference to the function func1 is resolved using the definition in MOD1.OBJ; the reference to the function func2 is resolved using the definition in MOD2.OBJ.
2.In MOD1.OBJ, the references to printf and scanf are resolved using the definitions in SLIBCE.LIB. The linker uses this library because it finds the library name within MOD1.OBJ.
3.In MOD2.OBJ, the references to myline and mycircle are resolved using the definitions in MYGRAPH.LIB.