CL Invokes the Linker

OverviewHow Do ICompiler Options

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.

Option Description
/c Compile without linking
/E, /EP, /P Preprocess without compiling or linking
/Zg Generate function prototypes
/Zs Check syntax

For further details about linking, see Linker Option Reference.

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 func1 in MOD1.C and the function 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.

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:

  1. In MAIN.OBJ, the reference to func1 is resolved using the definition in MOD1.OBJ; the reference to 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 the library that the linker finds named within MOD1.OBJ.

  3. In MOD2.OBJ, the references to myline and mycircle are resolved using the definitions in MYGRAPH.LIB.