CL Invokes the Linker
CL automatically invokes the linker after compiling unless the /c option is used. CL passes to the linker the names of .obj files created during compiling and the names of any other files specified on the command line. The linker uses the options listed in the LINK environment variable. You can use the /link option to specify linker options on the CL command line. Options that follow the /link option override those in the LINK environment variable. The options in the following table suppress linking.
|/c||Compile without linking|
|/E, /EP, /P||Preprocess without compiling or linking|
|/Zg||Generate function prototypes|
For further details about linking, see MSVC Linker Options.
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
func1in MOD1.c and the function
MOD1.c calls the standard library functions
MOD2.c calls graphics functions named
mycircle, which are defined in a library named MYGRAPH.lib.
To build this program, compile with the following command line:
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 in each .obj file. For more details, see Use Run-Time Library.
CL passes the names of the .obj files, along with the name MYGRAPH.lib, to the linker. The linker resolves the external references as follows:
In MAIN.obj, the reference to
func1is resolved using the definition in MOD1.obj; the reference to
func2is resolved using the definition in MOD2.obj.
In MOD1.obj, the references to
scanf_sare resolved using the definitions in the library that the linker finds named within MOD1.obj.
In MOD2.obj, the references to
mycircleare resolved using the definitions in MYGRAPH.lib.