News: - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Programming In VC++ - Command-Line Tools

Started by sivaji, Jan 10, 2008, 05:16 PM

Previous topic - Next topic


Programming Stuffs in "VC++" - Technical Skills for INTERVIEW

Command-Line Tools

Is using the wonderful tools of Visual C++ from the command line a heresy? I don't think so. Often it is much simpler to type cl myprog.c than to go through the elaborate process of setting up a Visual C++ project, adding your files to it, and recompiling and running your project through the graphical interface.

When would you use the command-line tools? I would like to offer some simple rules of thumb: If your project is complex enough to require a make file, use the graphical interface. If you wish to debug your program interactively, use the graphical interface. If your program has a resource file, use the graphical interface.

However, if you just want to key in a 10-line example from a textbook and quickly test it, you may find it more convenient to do so at the command line. In fact, many simple examples in this book can easily be compiled from the command line (of course, nothing prevents you from loading them as Visual C++ projects).

Many command-line tools depend on the appropriate Visual C++ directories being on the path and on other environment variables being properly set. If you are using Windows NT as your development platform, the Visual C++ installation program offered you the option to register these environment variables so they automatically appear in DOS sessions. If you are using Windows 95, you must run the vcvars32.bat batch file (found in your msdev\bin directory) to register these variables before you can use the command-line tools. Note that it may be necessary to increase the environment size for DOS windows (select the Properties command from the DOS window's control menu and use the Memory tab) before vcvars32.bat can be successfully run.

The vcvars32.bat batch file also accepts an optional parameter that specifies the target system (X86, Motorola 68k, or the PowerPC). Use this parameter to specify a target environment other than Intel X86 on systems with a Visual C++ cross-development edition installed.

The environment variables set by vcvars32.bat include the following: INCLUDE specifies the location of include files for the compiler; LIB specifies the location of library files for the linker; and PATH specifies the location of Visual C++ executables (in addition to any other directories that you may have included there). There are several other environment variables interpreted by the various utilities; for example, the compiler, cl.exe, reads the contents of the CL environment variable for additional command-line options, while the linker reads additional command-line arguments from the LINK environment variable. All Visual C++ programs make use of the TMP environment variable to find the location for temporary files.

The C/C++ Compiler

The Visual C++ compiler is invoked from the command line using the cl command. If invoked with the name of source files on its command line and no other parameters, it compiles those source files, then invokes the linker to create an executable.

If you specify the name of object files or library files on the command line, those will be passed to the linker. For example, consider the following compiler command line:
cl hello.c myfunc.obj mylib.lib

In response to this command, the Visual C++ compiler will compile hello.c, then invoke the linker with hello.obj and myfunc.obj. It will also pass the name of the library file mylib.lib to the linker, which will use this file in addition to any default libraries when searching for library functions.

If you wish to compile a file but not produce an executable, use the /c option:
cl /c hello.c

Note that using the forward slash or the dash is equivalent when specifying command-line options.

Some other useful options include /MT (link with the multithreaded version of the run-time library), /MD (link with the DLL version of the run-time library), and /LD (create a DLL). Of course, the compiler has a myriad other options (type cl /? to find out just how many). However, if you need to specify complex options, you're probably better off compiling from the Developer Studio.

The object files produced by the Visual C++ compiler are of the Common Object File Format (COFF).

The Linker

The linker, link.exe, is a tool that accepts Common Object File Format (COFF) files, 32-bit Object Module Format (OMF) files, library files, and other input files and produces Win32 executable files or dynamic link libraries. In its simplest form, the linker is invoked with the name of an object file on the command line:
link hello.obj

The linker accepts many command-line options. Among these is the /subsystem option that specifies the type of the resulting executable. For example, specifying /subsystem:windows forces the output file to be a Windows executable. However, often it is not necessary to use this option; the linker defaults to /subsystem:console if the object files contain a definition for the main (or the wide-character version, wmain) function, and to /subsystem:windows if a WinMain (or wWinMain) function exists.

If you wish to produce a dynamic link library instead of an executable file, use the /DLL option. This option is automatically specified when the linker is invoked by the compiler, and the compiler was run with the /MD option.

Many other options control how input files are processed, what default libraries are used, and the type and contents of output files. In additional to executables, the linker can also produce debugging files (such as map files); you can also specify whether the executable is to include debugging information or not.

The Library Manager

The library manager, lib.exe, is used to create libraries of COFF object files. It can also be used to create export files and import libraries for DLLs.

In its simplest form, lib.exe is invoked with a series of object filenames on its command line. It uses the base name of the first object file as the base name of a library file, and creates (or updates) a library consisting of the object files on its command line. The name of the output file can be overridden using the /OUT option.

If you wish to later remove the contents of an object file from the library, use the /REMOVE option. The /EXTRACT option can be used to extract the contents of the object file into a separate file.

To use the library manager to create an export file and an import library, use the /DEF option. Note that you rarely need to use this option, as the linker usually automatically creates the export file and the import library. Using the library manager is only required to resolve a situation when a program exports to, and imports from, the same library.

The Program Maintenance Utility

The program maintenance utility, or make tool for short (nmake.exe), is used to evaluate dependencies in make files and invoke the appropriate commands to generate targets. For example, a simple make file may look like this:
test.exe: test.obj

          link test.obj

test.obj: test.c

          cl /c test.c

In all make files, the make tool evaluates targets in the order of their dependencies and updates all targets if they are older than any of their dependencies.

In addition to targets and dependencies, make files can contain many additional features, including macros and inference rules. These features render make files a formidably powerful and versatile tool.

Important options for nmake.exe include /a (specifying that all targets should be rebuilt unconditionally), /n (specifying that the make tool should only display, but not execute, commands), and /f (specifying the name of a make file).

Other Command-Line Tools

Other command-line tools that ship with Visual C++ include rc.exe, bscmake.exe, dumpbin.exe, aviedit.exe, and editbin.exe.

The resource compiler, rc.exe, can be used to compile resource files and prepare them for linking with your project's object files. The resource compiler accepts the name of a resource file (with the .rc extension) and optional switches on the command line. Switches include /d (to define a symbol for the preprocessor), /fo (to specify the name of the output file), and /v (for verbose output). To obtain a complete list of command-line options, run rc.exe with the /? option.

The browse information maintenance utility, bscmake.exe, is used to create browse information (BSC) files from SBR files created during compilation. Browse information files can be viewed from the Developer Studio using its browsing facilities.

The binary file dumper, dumpbin.exe, displays information about COFF object files.

The aviedit.exe program is a simple tool to edit AVI files intended for animation controls. Use this tool to create an animation file from a series of bitmaps.

The binary file editor, editbin.exe, can be used to view and modify certain properties of COFF object files. Among these options is the ability to change a file's base address, the default heap size, and the default stack size.


The Microsoft Developer Studio is the centerpiece of the Visual C++ development system. It provides a series of different views on your projects through the Project Workspace windows; through this window, a project's class structure, files and dependencies, and resource file components can be examined.

A project workspace may contain one or more top-level projects; each project can have several subprojects, which can further be nested. For each project, Developer Studio maintains a series of configurations; a project configuration consists of settings used to rebuild the project. For example, when you create a new project using the Intel version of Visual C++, by default two project configurations (a debug and a release configuration) are created.

Source files in a project can be edited using the Developer Studio's customizable editor. Resource file components can be edited using the built-in resource editor; these components include accelerator tables, menus, dialogs, toolbars, icons, cursors, bitmaps, string tables, and custom resources.

Project settings can be modified using the Settings command in the Build menu. The Project Settings dialog is a complex dialog with eight property pages and several subpages. Settings for all configurations, specific configurations, a subset of files, or individual files can be specified or altered using this facility. Settings categories include the General, C/C++, Linker, Debug, Resource, OLE Types, Custom Build, and Browse Info categories.

Another useful Developer Studio facility is the InfoViewer; this facility provides access to MediaView titles, such as the Visual C++ on-line documentation or the Microsoft Developer Library.

The Developer Studio is also integrated with tools such as the AppWizard, ClassWizard, the integrated debugger, profiler, and source browser.

Many Visual C++ components can be used from the command line. For example, a basic Hello, World program can be compiled by typing cl hello.c. Other command-line tools include the linker, library manager, and the program maintenance utility (make tool).
Am now @ Chennai

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview