Incremental build of C++ projects fails under Team Build 2008

I’ve been working with some folks on the MSDN Forums around this issue and thought it would be good to put together a blog post that describes the    issue and solution that seems to work.  Here’s a link to the MSDN Forum thread.

The Problem:cpp

You have a C++ solution running under Team Build 2008.  Team Build is configured for incremental builds.  The build process fails to detect that a dependency has changed if that dependency is a .h file.  The resulting build is incorrect.

For example, you have the following files in your project:

  • common.h
  • foo.cpp – includes common.h
  • bar.cpp – includes common.h

You make a change to common.h and foo.cpp then trigger a Team Build that has IncrementalBuild=True.  The result is that foo.cpp is recompiled but bar.cpp is not.

The Issue:

The VC++ compiler is expecting that certain environment variables are set at run-time.  During a Team Build, these are not being set and the VC++ compiler is behaving erratically.

How to determine if this is your problem:

Build your solution using MSBuild at the command-line from both a VS command prompt and from a non-VS command prompt.  The incremental build should run correctly from the VS command prompt but not from the non-VS command prompt.

The Solution:

  1. Make sure you have the following environment variables set on the build machine and that they are the same as the values used when you run from the VS command prompt: INCLUDE, PATH, LIB, and LIBPATH
  2. You can use the MSBuild Extensions Pack’s EnvironmentVariable custom task to set these environment variables during your build.
  3. In your Team Build, set the VCBuildUseEnvironment property to True.  This will cause the VCBuild task to use the machine’s environment variables.

You might also be able to run the vcvars32.bat file during your build to set the environment.  This would replace Step 2 above.

Final Comments:

The key to the whole thing is ensuring that the environment variables are correct and using the VCBuildUseEnvironment property to force the VC++ compiler to use them.  If the environment variables are not set at the machine level or at the User level for the build account then the build will still exhibit the issue. 

Hopefully this will simulate the conditions that occur when your run under the VS Command Prompt.  Please leave a comment if this works for you.

– Steve

Updated 22-Dec-2009: Updated with comments from Microsoft folks.