Using Microsoft Windows 7 SDK on Linux

It is sometimes convenient to be able to run Microsoft's C++ compiler on Linux. For instance, if you're developing Wine conformance tests, and you want to make sure they'll compile with MSVC++, it's nice to simply be able to type
cl -DSTANDALONE   -D_X86_ lzexpand.c lz32.lib
at the Bash prompt.

Downloading and Installing the Microsoft Windows 7 SDK and DirectX SDK

Thanks to winetricks, this is easy... once you know what works. The magic command is
  WINEPREFIX=$HOME/.local/share/wineprefixes/psdkwin7
  export WINEPREFIX
  wget http://kegel.com/wine/winetricks
  sh winetricks -q psdkwin7 dxsdk_nov2006
You should select just the minimum core subset of the platform SDK, or it might not install properly (see bug 21596). If you're building wine from git, you may need to patch it to avoid bug 30845.

Setting up cl

To use cl from the bash prompt in Linux, you'll need a short scripts with that name in ~/bin which just runs it tools in Wine. A minor complication is how to get environment variables set properly. Normally with MSVC, one does this by executing the batch file VCVARS32.BAT, but it's just as easy to set the variables in a shell script manually.

Here's the ~/bin/cl script I use:

#!/bin/sh
WINE=${WINE:-wine}
WINEPREFIX=${WINEPREFIX:-$HOME/.local/share/wineprefixes/psdkwin7}
export WINEPREFIX
PROGRAMFILES="c:\Program Files"
WSDK="$PROGRAMFILES\Microsoft Visual Studio 9.0"
WPSDK="$PROGRAMFILES\Microsoft SDKs\Windows\v7.0"
WDXSDK="$PROGRAMFILES\Microsoft DirectX SDK (August 2006)"
export WINEPATH="c:\windows;c:\windows\system32;$WSDK\Common7\IDE;$WSDK\VC\bin"
export INCLUDE="$WSDK\VC\include;$WPSDK\Include;$WDXSDK\Include"
export LIB="$WSDK\VC\lib;$WPSDK\Lib;$WDXSDK\Lib\x86"
$WINE cl.exe $@
I constructed this iteratively by starting out with setting no variables, seeing what cl complained about when I tried to compile/link a program, using 'find' to locate the missing files in ~/.wine, and adding the missing directory to the appropriate variable.

Once you've created this, add ~/bin to your PATH, e.g.

PATH=${PATH}:${HOME}/bin
so you can run cl from the commandline without a path.

Try building "hello world" now!

Compiling a DirectX9 C++/HLSL example

Some C++/HLSL examples on the web, for instance, the ones at codesampler.com, still ask for shader model 1.1... but the DirectX9 runtimes released since Dec 2006 only support shader model 2.0 and above, so you'll get a very clear error when you try to run them. In my quick test, just changing 1_1 to 2_0 where it occurred in the source files was enough to get the example running.

Here are the commands to download and build a sample app (assuming you've installed the SDKS, created ~/bin/cl and put it on your PATH, as described above.) (Note: the same commands can be used in Cygwin on Windows, though there you would append to PATH rather than setting WINEPATH, and you don't need a cl script.)

  wget http://www.codesampler.com/source/dx9_hlsl_simple_vs2ps.zip
  unzip dx9_hlsl_simple_vs2ps.zip
  cd dx9_hlsl_simple_vs2ps
  cl dx9_hlsl_simple_vs2ps.cpp user32.lib gdi32.lib d3d9.lib d3dx9.lib
  wine dx9_hlsl_simple_vs2ps.exe
  # If it complains about shader model 1_1, edit the .cpp file, change 1_1 to 2_0, and run cl again.