http://www.grasp.upenn.edu/~mcken/grasplab/hints/msvc.html

 

Microsoft Visual Studio and C++

Documentation

To build programs from a command shell

You can use the Visual C++ compiler cl.exe directly from a command shell, much as you would use a Unix compiler. First, set the shell's environment variables INCLUDE, LIB, and PATH to record the following directories:

Variable Path(s)
INCLUDE d:/pkg/VisualStudio/VC98/include
LIB d:/pkg/VisualStudio/VC98/lib
PATH d:/pkg/VisualStudio/VC98/bin
d:/pkg/VisualStudio/Common/MSDev98/bin

For a t-shell, add these to your Windows startup file .tcshrc-windows. For a bash shell, it seems you must set INCLUDE and LIB before starting bash. You can simply assign them via the Control Panel.

Use your favorite text editor, such as Emacs, to edit your source files. You'll probably want to add the GNU/Cygwin utilities to your path, too.

To see a list of options for cl.exe, open the Microsoft Visual C++ 6.0 IDE, select Help -> Index from the menubar, and type "CL options" for the keyword to find.

To build programs using a C/C++ API

The Lab provides several C/C++ APIs, including MIL, ImageMagick, Intel libraries, OpenGL/GLUT, and VTk. To build and run programs using an API, you usually need to specify the location of header and library files for the compiler and the system run-time linker. Here's the general procedure for a fictitious API, called "api." For clarity, let's suppose it is installed under p:/api and that our application requires its header file p:/api/api.h, LIB file p:/api/api.lib, and DLL file p:/api/api.dll. The big picture:

  1. Put the following line in each source file that uses the API: #include <api.h>
  2. Tell the compiler to look for the header file api.h in directory p:/api.
  3. Tell the linker to search p:/api/api.lib when resolving symbols.
  4. Add p:/api to your shell's PATH variable so the run-time system can find api.dll.

Here's an example for building program main.exe from main.c via the command line. The C source file calls functions from the API, which it declares by including api.h. To compile main.c into main.obj without linking:

   -> cl -c main.c -Ip:/api
The include directive in the source file tells the compiler to look for and use the header file aph.h of the API. The switch "-Ip:/api," in turn, tells the compiler to look in directory p:/api when searching for header files. Thus the compiler finds and includes p:/api/api.h.

Next, to build main.exe from main.obj:

   -> cl -Femain.exe main.obj p:/api/api.lib
This tells the linker to look in library file p:/api/api.lib when resolving symbols, like functions, not defined in main.obj. Thus the linker finds the functions provided by the API. (For static libraries, the linker finds and incorporates the object code into the executable. For dynamic libraries, the linker incorporates stub object code that loads the actual library object code at run time.) You can also specify the search path and the library separately, like so:
   -> cl -Femain.exe main.obj -LIBPATH:p:/api api.lib
The switch "-LIBPATH:p:/api" tells the compiler to look in directory p:/api when searching for libraries specified on the command line. Thus it finds and searches p:/api/api.lib. This approach is convenient when linking against multiple libraries from the same API.

Of course, you can combine these two steps into one:

   -> cl -Femain.exe main.c -Ip:/api p:/api/api.lib

If you prefer, you can add p:/api to your shell's INCLUDE variable and omit "-Ip:/api" above. Similarly, you can add p:/api to your shell's LIB variable and omit the path "p:/api" from "p:/api/api.lib" or omit "-LIBPATH:p:/api." Under a t-shell, for example:

   -> setenv INCLUDE "${INCLUDE};p:/api"
   -> setenv LIB "${LIB};p:/api"
   -> cl -Femain.exe main.c api.lib

When builing programs from within Visual Studio, you specify the header and library information under "Project -> Settings." For compiling, click the C/C++ tab, select Preprocessor from the Category box, and enter p:/api in the "Additional include directories" box. For linking, click the Link tab and enter p:/api/api.lib in the "Object/library modules" box. Alternatively, you can enter only api.lib in the this box, choose Input from the Category box, and list p:/api in the "Additional library path" box.

Finally, you need to adjust your shell's PATH variable if the API uses a DLL. If you run from a t-shell, for example, adjust your path like this.

   -> setenv PATH "${PATH};p:/api"
Or, add this path in the startup files. If you run your program from Visual Studio, use the System applet in the Control Panel as you would to set your HOME variable. Adjust your path before opening Visual Studio.

Generally, an API puts its header and library files in separate subdirectories include and lib of its main directory. The aggregate build command would look something like this:

   -> cl -Femain.exe main.c -Ip:/api/include -LIBPATH:/p:/api/lib api.lib
See the API's hints for details on what paths to list.

Trouble saving files

If Visual Studio reports that it cannot save files to the h: drive, make sure that you have sufficient quota. If it cannot save to a network drive mapped to a project directory, make sure that directory's disk has sufficient free space. If it cannot save to a local disk drive, make sure that disk has sufficient free space. In all cases, you must have suitable write access to the file or its directory, of course.

Use the i: drive in Visual Studio IDE (obsolete)

[The Lab's h: drive is no longer sensitive to case, so the \\grasp\homeci share and the i: drive are no longer necessray or supported. These notes are retained for the record.

When using the Visual Studio IDE (integrated development environment), specify the i: drive instead of the h: drive to access your Grasp home directory. To do so, you must map i: to \\grasp\homeci (just once).

The map to your home directory on Grasp through the h: drive is case-sensitive, in consistency with Grasp's unix file system. For some reason, Visual Studio does not preserve case in file names; it appears to change file names to upper case. Consequently, Visual Studio usually cannot find files you specify with the h: drive. To get around this foolishness, file requests through i: match different cases in file names if the original request is not found.

By the way, the i: drive is a convention for the map to \\grasp\homeci. (Mnemonics: "i" for "insensitive"; "i" follows "h"; "ci" for "case-insensitive") You can choose another letter if you prefer.]

Error spawning cl.exe (obsolete)

[The Lab PCs have been re-configured to prevent this error. These notes are retained for the record.

If you get this error, your roaming profile records an incorrect directory for the Microsoft Visual C++ installation on the PC. Consequently, Microsoft Visual C++ cannot locate the compiler executable. The problem occurs because Microsoft Visual C++ is not yet installed in a uniform directory across PCs. This inconsistency is being resolved actively, but slowly. In the mean time, here's a fix:

  1. Exit Microsoft Visual C++.
  2. Select Start -> Run... and enter regedit. This starts the registry editor.
  3. Carefully navigate down this path:
       H_KEY_CURRENT_USER
          Software
             Microsoft
                DevStudio
                   6.0
                      BuildSystem
                         Componenets
                            Platforms
                               Win32 (x86)
                                  Directories
    
  4. Carefully right-click on the Directories key and choose Delete.
  5. Exit from the registry editor.]

Posted by '김용환'
,