Code analysis with Visual C++ Express Edition

I’ve blogged before about how much I like Visual C++ Express Edition, but it does lack one feature that I consider to be extremely important: code analysis using /analyze (i.e. PREfast). I pointed this out a while ago, and Michael Howard responded a couple of days later that the Vista PSDK compilers do support /analyze.

I finally got around to testing this theory out recently, and it works like a charm. With a little bit of one-time work, you can set this up for all of your projects. Here’s how:

  1. Get the latest Vista Platform SDK. This is available to beta testers via Connect and also via various other locations. Apparently this has been a part of the Vista SDK for a while, so even an older version should work, although I just tested with RC2 this weekend.
  2. Create a new configuration for your project in the IDE. I named mine Analyze. I copied all of my settings from the Debug configuration.
  3. Open up the property manager for the Analyze configuration and find the C++ command line section. Add /analyze to the command line. If you want to not count PREfast warnings as errors, you can also add /analyze:WX- in addition to /analyze. WX needs to be capitalized.
  4. Set the executable path using Tools->Options. Simply registering the SDK with Visual Studio is not enough – you have to actually manually add the compiler-specific BIN, LIB, and INCLUDE directories under the VC subdirectory of the PSDK.
  5. Finally, I had to change my system’s PATH environment variable to get Visual Studio to actually invoke the PDSK’s build tools in preference to its own.

I was able to get it working using both the IDE and vcbuild.exe by following this procedure. Compiling with /analyze takes a lot more time, and I’ve found that the PSDK headers aren’t actually quite PREfast-clean yet in a few places. There are also some bogus warnings about various APIs that return valid data lengths (i.e. recv()), but I had no trouble suppressing them with one-line warning suppression statements.

Using PREfast takes some getting used to, but in the end, I find it to be worthwhile. Oh, and incidentally, you can also get other features left out of Express Edition this way, like x64 compilers. Happy coding!

2 Responses to “Code analysis with Visual C++ Express Edition”

  1. I’m not sure if the express version supports it, but DEVENV.EXE also takes a command line parameter /USEENV which uses PATH,INCLUDE,LIBS,LIBPATH and SOURCE[S?] to populate the directory values. From a batch script it’s pretty easy to launch DEVENV for either VC, DDK or PSDK tools. The only thing missing is the ability to populate a specific platform’s directories so to switch you have to restart with different values for the env vars.

Leave a Reply