SCons with Microsoft Visual Studio Express 2008

Current Repository Revision: 8

I was visiting my parents this Easter, when I thought I’d try out the current development code on their Windows XP based computer. I installed Microsoft Visual Studio Express 2008 (C/C++), Python (version 2.6.1) and the latest stable SCons Windows installer (version 1.2.0).

After installing TortoiseSVN, I checked out the latest version (revision 6) of the example code, opened a command prompt, navigated to my mi_test_scons directory, and typed:

 scons -Q

It built the object files fine, but then I got to the linking stage, and the build was halted with the message:

LINK : fatal error LNK1104: cannot open file 'kernel32.lib'

Obviously my environment isn’t set up properly, I thought, so I searched for the vcvars32.bat file  which I have used in the past to set up the MSVS environment. I found this file (or rather its equivalent – vsvars32.bat) in the C:\Program Files\Microsoft Visual Studio 9.0\Tools directory. I ran it, and ran SCons again. The same error appeared.

Next I thought that, rather than use a standard command prompt, I’d use the Visual Studio 2008 Command Prompt available from the Microsoft Visual C++ 2008 Express Edition > Visual Studio Tools folder available on the Start Menu. Again I navigated to my mi_test_scons directory, and ran SCons. The same error appeared yet again.

I typed the link command that SCons said it was executing directly at the command prompt:

link /nologo /OUT:build\release\foobar\bar\tests\bar_test.exe
/LIBPATH:build\release\foobar\lib /LIBPATH:src\foobar\lib
foobar.lib build\release\foobar\bar\tests\bar_test.obj

This time, the link worked fine, so there had to be a difference between the environment SCons was using, and the MSVS environment.

After dumping out the SCons environment and looking at the command prompt environment, it was obvious there was a difference in the contents of the LIB variable. Specifically, the location of the Microsoft SDK directories was not being detected correctly by SCons. This is due to some assumptions made by SCons (that the SDK directories lie below the Visual Studio directories) that don’t hold true for Visual Studio 2008 Express. In 2008 Express Microsoft have separated the SDK from Visual Studio, so it is in its own directory tree.

From the SCons site it seems that this problem may be in the process of being fixed (and possibly has been fixed in the latest (non-stable) release). However, I only use stable releases, and I have older versions of SCons I want this to be able to work with, so I decided to add a quick workaround into the MereIdea code to  grab the LIB environment variable (and INCLUDE as problems may crop up there later) and use that in the SCons environment:

# If the compiler is msvs and the LIB and INCLUDE environment
# variables in the os exist, use them instead of SCons version
# to fix MSVS 2008 Express problems
if cmplr=='cl':
  envlib = os.environ.get('LIB')
  envincl = os.environ.get('INCLUDE')
  if envlib is not None:
    env.Append(ENV = {'LIB' : envlib})
  if envincl is not None:
    env.Append(ENV = {'INCLUDE' : envincl})

The above code fixes the problem with Visual Studio 2008 Express, and is now committed as revision 8 (revision 7 included an incorrect comment, corrected in revision 8). You have to remember to either run vsvars32.bat before running SCons or (my preferred option) use the Visual Studio2008 Command Prompt.

The next problem is to see how SCons handles having Visual Studio 2005 Express and 2008 Express installed on the same machine, and being able to select either to use for the build.

Leave a Reply

You must be logged in to post a comment.