Microsoft Compiler Setup and Configuration

From CS Support
Jump to: navigation, search

Installing the Microsoft Windows SDK and c/c++ build tool chain

In this short document I am going to show you how to set up the Microsoft proprietary compiler. The full c/c++ compiler (and associated tool chain) is available for free as part of the Microsoft SDK package. I will assume basic knowledge of the build process, i.e. you know (in general) what the lib and include paths are.

Why would you want to do this? Well I can think of a few reasons.

  • Perhaps you are already familiar with the build process in a Unix/Linux system and want to extend that experience onto a Windows system.
  • Perhaps you do not want to bother with installing Visual Studio (a HUGE piece of software).
  • Perhaps you are in an environment where discreet makefiles are required (as opposed to Visual Studio .sln project/solution files).
  • Perhaps you are tasked with learning how the build system works so that you aren't relying on clicking the magic “build” button in Visual Studio to compile your software

Installation

To obtain the compiler, we are going to download and install the Windows SDK. Note that in the current version, the Windows Platform SDK (PSDK), and various other SDK's (such as the .NET SDK) have been merged into a single SDK. If you already have Visual Studio installed, you already have the Windows SDK, so you can skip ahead to the configuration step.

The Windows SDK installer is available for download directly from Microsoft via: [1]

Note that at the time of writing, the current version (SDK – Aug 2009) supports:

  • Windows XP, Vista, 7 (separate SDK's for Windows Server are available)
  • Architectures: x86, x86_amd64, IA64
  • .NET Framework v.2.0, 3.0, 3.5, 3.5sp1

Download and run the installer (winsdk_web.exe). Accept the license, default installation locations, and default installation options. The SDK will be downloaded and installed. After the installer completes you may notice two additional folders appear in your program files folder: Microsoft SDKs and Microsoft Visual Studio 9.0 (version numbers will change in the future). Note that Visual Studio itself was not installed.

Configuration

Configuring the Microsoft proprietary compiler is quite simple. The compilation system relies on a set of environment variables to instruct it where to find shared libraries, include files, and a host of other things. The SDK provides you with a set of batch files that will pre-setup a temporary build environment for whichever target architecture you wish to compile under. These batch files are typically found in (C:\Program Files\Microsoft Visual Studio 9.0\VC\bin). I am going to show you how to set up a more permanent build environment such that you can invoke the compiler inside any console terminal you happen to be in. There are three environment variables we need to create or modify: LIB, INCLUDE, and PATH. Each of these environment variables contains a semicolon separated list of directories.

LIB – the LIB environment variable tells the compiler where to search for shared libraries. There is also a command line switch you can give the compiler to specify library locations, but we are interested in specifying the base set of libraries: the standard libc and STL libraries as well as the libraries to support the Microsoft Windows API. The directories we need to add to the LIB environment variable are:

  • C:\Program Files\Microsoft Visual Studio 9.0\VC\lib
  • C:\Program Files\Microsoft SDKs\Windows\v7.0\Lib

(again, realize that these are the locations at the time of writing, version numbers will change in the future).

INCLUDE – the INCLUDE environment variable tells the compiler where to look for common header files (such as stdio.h for the statement #include <stdio.h>). Again, there are two locations we need to add to the environment variable:

  • C:\Program Files\Microsoft Visual Studio 9.0\VC\include
  • C:\Program Files\Microsoft SDKs\Windows\v7.0\Include

In the first of these you will find the standard c and c++ header files (stdio.h and iostream.h for example). In the second directory you will find header files specific to the Microsoft Windows API.

You could add more directories to these two environment variables to add any additional libraries you may require. A very good example of this would be adding a set of Windows pthreads libraries (such as the pthreads-win32 libraries). If the libraries in question are very project specific (such as a physics engine or something like that), you could also use the compiler command line switch to tell the compiler/linker about other library locations.

PATH – the PATH environment variable tells Windows where to look (as well as the order to look) for stuff when it needs to find something. Simple examples of what Windows might need to look for include program binaries (such as the compiler itself) and DLL's containing routines an application was dynamically linked against. It is very likely you already have a path environment variable. If this is the case, you need to take care to modify it and not overwrite it. If you overwrite your path environment variable, it is very likely that a lot of your applications will stop working. There are three directories we need to add to our path:

  • C:\Program Files\Microsoft Visual Studio 9.0\VC\bin
  • C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
  • C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin

The first of these directories contains your common build tool chain: compiler – cl.exe, assembler – ml.exe, linker – link.exe, as well as Microsoft's implementation of make – nmake.exe. The second directory contains many utilities for developing under the Windows API. There is an extensive amount of utilities in this location so I wont go into detail what everything in there does – read the kb. The third directory is the location of the ubiquitous mspdb80.dll. This library contains hooks for the compiler to interface with Visual Studio (which it runs as part of its normal operation). If you execute the compiler and get a warning that mspdb80.dll was not found, you very likely do not have this location in your PATH.

Having set up the preceding environment variables you can now compile a simple c source file. The following snippet of terminal output shows you the compilation of a very simple c program.

Program to compile

 #include <stdio.h>
int main(int argc, char **argv) { printf("Hi, planet!\n"); return 0; }

Invoking the compiler

 C:\Documents and Settings\sam\Desktop>cl hello.c
 Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
 Copyright (C) Microsoft Corporation.  All rights reserved.
hello.c Microsoft (R) Incremental Linker Version 9.00.30729.01 Copyright (C) Microsoft Corporation. All rights reserved.
/out:hello.exe hello.obj

And running our compiled binary

 C:\Documents and Settings\sam\Desktop>hello.exe
 Hi, planet!

That will get you off and running with Microsoft's discreet build environment. You can now develop software and use makefiles to intelligently manage your build process. Note that this isn't the ONLY way to compile c/c++ code in Windows, but it is the native Microsoft way. The reader is encouraged to research both MinGW and Cygwin and understand the similarities, differences, benefits, and drawbacks between each of these three build environments.


~Sam Hansen, Systems Administrator

Email comments/corrections to cs.support@wwu.edu