Building Blender With MSVC Using Cmake

If there is an problem when trying to build, I won’t be able to help beyond what is written here. Since moving to the non-commercial Houdini Apprentice HD I am no longer building Blender, nor am I keeping up with the changes to the Blender source. As such, the below information is valid as of early April 2008. If an error occurs, you might want to post your questions to the blender.org forum where someone might be able to help track down the issue, fix the source, or fix the CMake files.

Firstly, I’ll say that just because this is using a Microsoft product to compile doesn’t mean this is a mediocre compiler. There are many companies that use Visual Studio to create their million dollar-generating programs and I think you’ll find there is good reason – it’s a really good compiler with great (working) optimizations. Secondly, I describe here using the free Visual C++ 2008 Express (VC9), which isn’t all that different from the for-money Visual Studio. Sure there are a few optimizations that Visual Studio has that VC9 doesn’t, but VC9 compiles just like Visual Studio.

On to what you came here for! There is a lot needed to get this going. You will need to install a whole bunch of big things to get a build, but I think using MSVC makes a far better build since optimizations actually seem to work (whereas with Scons, they don’t with Intel chips) and you have the AVI codecs, OpenEXR, FFMPEG, Quicktime, Blenderplayer, and all manner of other goodies. This process might seem long, but the length is due to the initial setup. Once you’ve done the setup, subsequent builds are really easy to make. If you’re new to building and are looking to build Blender quickly and easily, check out Mike Pan’s tutorial on using Scons.

PART 1 (Getting Setup)
PART 2 (Getting The Sources)
PART 3a (Put the Quicktime Libraries in the Blender lib folder)
PART 3b (OpenEXR lib Caveat for Blender 2.45 only)
PART 4a (Using CMake to create project files)
PART 4b (OpenMP CMake Option)
PART 5 (Optimization – Optional)
PART 6 (Building)
PART 7 (Tips) – includes info on manifests, optimization issues, and debug builds

PART 1 (Getting Setup)

You will need the following:

It is imperative that after installing Visual C++ 2008 Express, you check to see if the paths specified below are there (they will look like $(WindowsSdkDir)include), and if they aren’t you should add them as follows:

Update the Visual C++ directories in the Projects and Solutions section in the Options dialog box. Add the paths to the appropriate subsection:

Executable files: C:Program FilesMicrosoft SDKsWindowsv6.0Bin

Include files: C:Program FilesMicrosoft SDKsWindowsv6.0Include

And C:Program FilesMicrosoft SDKsWindowsv6.0Includegl

Library files: C:Program FilesMicrosoft SDKsWindowsv6.0Lib

(For the Vista SDK, remember to select the x86 libraries during installation so they are installed!)

If you’ve installed DirectX after VC then this next step will likely be done for you, but it’s good to check. Here is the manual method. Update the Visual C++ directories in the Projects and Solutions section in the Options dialog box. Add the paths to the appropriate subsection:

Executable files: C:Program FilesMicrosoft DirectX SDK (%Date%)Developer Runtimex86

Include files: C:Program FilesMicrosoft DirectX SDK (%Date%)Include

Library files: C:Program FilesMicrosoft DirectX SDK (%Date%)Libx86

  • For the DirectX SDK, you could download and install the entire thing, but all you need are the files that I mention below that can go into the PlatformSDK folder. Of course, the only other way to get the files from the ~500 MB installer (other than installing it) is to have them from another source.

PART 2 (Getting The Sources)

Option 1: Tortoise SVN

First, you’ll need the source. The source uses SVN so you’ll have to pick the client of choice in your OS to get the sources. As a Windows user I use TortoiseSVN. It’s crazy easy since it’s a shell program and all you have to do is right click in a folder named “blender,” select SVN checkout, and use the following location:

https://svn.blender.org/svnroot/bf-blender/trunk/blender

For the libraries, in another folder named “lib” (on the same level as your new blender folder) with another folder in it called “windows,” checkout using:

https://svn.blender.org/svnroot/bf-blender/trunk/lib/windows

You will probably see an error message; this informs you about the certificate that is not issued by a trusted authority. However the certificate is fully functional and will ensure secure transit of the data. You can permanently accept this certificate. After accepting the certificate the sources will be checked out for anonymous users.

For particular releases, you would use a different project name. For example:

https://svn.blender.org/svnroot/bf-blender/tags/blender-2-45-release*

Option 2: Direct Download

Or you download the release tarball (~13.5MB) from here and extract it. You will then have a folder with the source of the current release.

PART 3a (Put the Quicktime Libraries in the Blender lib folder)

If you intend to turn Quicktime on using CMake (you are not required to), we need to add the Quicktime include and library folders to the Blender lib folder. From the QuickTime 7 SDK install folder copy the Libraries folder and the CIncludes folders to the following locations in the sources (you’ll need to create the QTDevWin folder):

%Your Blender Source Folder%/lib/windows/QTDevWin/Libraries
%Your Blender Source Folder%/lib/windows/QTDevWin/CIncludes

PART 3b (OpenEXR lib Caveat for Blender 2.45 only)

*If you’re building an SVN version or any version higher than 2.45 you don’t need to do anything.*

Using the openexr libs that comes with Blender (in the openexr lib_msvc folder) causes builds to fail (the errors are something like msvcprt.lib(MSVCP90.dll) : error LNK2005). This is because the openexr libs in the lib_msvc folder (which CMake is set to use) weren’t built properly. You’ll find that that the Half.vcproj project in the OpenEXR source files had the preprocessor definitions _USRDLL and _DLL which basically forced it to build as /MD unless these definitions are removed. These definitions were interfering with the switch from /MD[d] to /MT[d], and “pulling in” extra symbols (and the DLL version of the CRT with #pragma comment(lib,”msvcprtd”)). Ignoring this library (with /nodefaultlib) then leaves the (aforementioned redirection) operators unresolved.

To resolve this you can 1) replace the libs with ones you’ve built removing preprocessor definitions _USRDLL and _DLL from Half.vcproj. OR 2) rename or delete the lib_msvc folder in the %Blender Source Folder%/lib/windows/openexr folder, then rename the lib_vs2005 folder to lib_msvc.

PART 4a (Using CMake to create project files)

Hopefully you’ve already downloaded CMake as either an executable or zip file and installed CMake or unzipped it. So I don’t re-write things that have already been said, here is the wording from the CMake site:

Run CMakeSetup.exe, which should be in your Start menu under Program Files, there may also be a shortcut on your desktop, or if you built from source, it will be in the build directory. A GUI will appear… similar to what is shown below. Select Visual Studio 9 as the file export type. The top two entries are the source code and binary directories. They allow you to specify where the source code is for what you wanted to compile and where the resulting binaries should be placed. You should set these two values first. If the binary directory you specify does not exist, it will be created for you. The Build for option, allows you to select which type of build files are generated.

The cache values area is where you can specify different options for the build process… More obscure variables maybe hidden, but can be seen if you click on the “Show Advanced Values” button. Once you have specified the source code and binary directories you should click the Configure button. This will cause CMake to read in the CMakeLists.txt files from the source code directory and the cache area to be updated to display any new options for the project.

Adjust the components to ON or OFF if desired and click the Configure button again. New values that were caused by the configure process will be colored red. To be sure you have seen all possible values you should click Configure until no values are red and you are happy with all the settings. Once you are done configuring, click the OK button, this will produce Microsoft Visual C++ workspaces and exit CMakeSetup.exe.

CMakeSetup.exe generates the build files in the binary directory you specified. … A MSVC workspace file is created. Typically this file has the same name as what you are compiling (e.g. blender.sln)… The next step in this process is to open the workspace with MSVC. Once open, the project can be built in the normal manner of Microsoft Visual C++. The ALL_BUILD target can be used to build all of the libraries and executables in the package.

PART 4b (OpenMP CMake Option)

There is OpenMP support in Blender now and it is an option in CMake. If you have Visual Studio and set this to “TRUE” in CMake, in VC9 for all the projects you need to:

Open the project’s Property Pages dialog box.
Click the C/C++ folder.
Click the Language property page.
Modify the OpenMP Support property.

Unfortunately, the free Visual C++ Express doesn’t have OpenMP support. So unless you have Visual Studio or try some other methods, you won’t be able to incorporate OpenMP into your build. If you are using Express, you’ll need to set this option in CMake to FALSE.

PART 5 (Optimization – Optional)

If you want to optimize your build, you can either use MSCV or Scons to optimize and builld. I will discuss how to optimize in both, but I will focus on building with MSVC only.

With MSVC:

Here are two flags that I have found dramatically decrease render times when used inside MSVC:

  • /arch:SSE or /arch:SSE2

Open the project’s Property Pages dialog box.
Click the C/C++ folder.
Click the Code Generation property page.
Modify the Enable Enhanced Instruction Set property.

  • /fp:fast

Open the project’s Property Pages dialog box.
Expand the Configuration Properties node.
Click the C/C++ folder.
Click the Code Generation property page.
Modify the Floating Point Model property.

*Note that /fp:fast for the bl_elbeem project (fluids) and the src will cause VC8 to crash. If you use VC9 to compile you can use /fp:fast on these two projects, however doing this to the source project causes the YafRay plugin not work unless xml is turned on and and I’ve seen it crash the elbeem project. Keeping these two projects at /fp:precise solves this issue. I also advise using the /O2 flag, however this is usually on by default and you can check if it is by looking at the top of the Code Generation property page.

You will need to do these flag modifications to every project that allows these modifications (All_Build dosen’t have these options available). What speeds this process up is to open the properties of the top project then Shift+Click the bottom project that can take flags. This causes all those projects (including the ones you’ve already selected) to be selected. You can then modify the properties window you have open and have all the selected projects be affected. For the list of available flags check out Microsoft Library.

  • /LARGEADDRESSAWARE

Open the blender and blenderplayer project Property Pages dialog box.
Click the Linker folder.
Click the System property page.
Modify the Enable Large Addresses property.

What /LARGEADDRESSAWARE does is allow more virtual memory to be used for the 32-bit program on either a 32-bit or 64-bit Windows. From Microsoft: “Virtual address space per 32-bit process in a 32-bit system is 2 GB… 3 GB if the system is booted with the /3GB switch [and the application is compiled with the /LARGEADDRESSAWARE switch]. For 64-bit systems it’s 2 GB… 4 GB if the application is compiled with the /LARGEADDRESSAWARE switch.” For those not familiar with where it should go in the boot.ini file of 32-bit systems, see here. WARNING: Mike Pan (yes, the Mike Pan) was the one who first pointed me to this flag, and he’s the one who has tested it the most. He says this regarding the flag: “The new limit seems to be much less than 3GB, possibly only around 2.2GB, but it is still better than before… Also, the /3GB flag seems come with a price, it’s been causing some issues with certain applications, especially games where sometimes everything would just freeze or drops down to really low performance.”

Now on to building.

PART 6 (Building)

  • Open %Your CMake made project file binaries folder%blender.sln.
  • Select [Release] in combo box on the top toolbar. If you want a debug build, select [Debug].
  • Build solution (F7).
  • The Blender release (blender.exe and/or blenderplayer.exe can now be found in %Your CMake made project file binaries folder%binrelease with all the required folders, dlls, and scripts. There are a few build related files you don’t need, like the blender and blenderplayer .exp, .lib, and .idb files, and the mkprot.exe and makesdna.exe files. This release requires either the x86, x64 and Itanium-based versions of Windows Server 2003 SP1, Windows XP SP2, Windows XP x64 Pro Edition, Windows 2000, or Windows Vista with version 2 or above of the .Net framework (since it installs the VC9 dlls). For a way to have Blender run on XP SP1 or machines that don’t have version 2 or up of the .NET framework, see the first building tip below.

There you have it! Hopefully I’ve saved you some time having written it out here. If you have any problems, feel free to contact me. And if you’re looking to patch Blender, check out my patching Blender tutorial or use Tortoise SVN to apply patches.

PART 7 (Tips)

With the instructions above, Blender can be built with no problems. But after the first build and with continued use, you may find that the build won’t run on certain machines, the build will fail, or it won’t take the optimizations you set. I have outlined below some cases when this might happen and what to do about it.

  • You can run the build fine but it won’t run on a different computer (you get an error when you run the build).

This is usually due to the Visual C and C++ runtime dlls not being located on the system that can’t run the build. I think (but cannot yet substantiate) that if you have version 2 of the .NET Framework, you should be fine. In any case, if it doesn’t run, you can either place the msvcr90.dll, msvcp90.dll , msvcm90.dll files from windowswinsxsx86_microsoft.vc90.crt_*really long string* into the folder where your blender.exe is, or you statically link these files to the blender.exe (make the runtime dlls get packaged with it by doing the following:

1) On the machine you have Express installed, create the following folder and subfolders in your program filesmicrosoft visual studio 9VC folder: redistx86Microsoft.VC90.CRT

2) Copy msvcr90.dll, msvcp90.dll , msvcm90.dll from windowswinsxsx86_microsoft.vc90.crt_*really long string* into the folder program filesmicrosoft visual studio 9VCredistx86Microsoft.VC90.CRT

3) in the above Microsoft.VC90.CRT folder, create a new file named: Microsoft.VC90.CRT.manifest

4) Paste the following content into the above manifest file:

manifest text – right click and save as since WordPress is giving me issues with putting this particular code on the page or opening it directly in a text file.

*Make sure the version number matches the version number in the dll file description. Now you have a new folder in your Visual C++ Express installation that can be re-used when ever you need it.

5) You could stop here, but I copy this Microsoft.VC90.CRT folder to the same place as the blender and lib folder of the blender source so it’s easier to write the path of the manifest file (as below).

6) Right click on the blender project file and go to Linker > Manifest file. In the Generate Manifest line select No. Then go to the Additional Manifest Dependencies line and path to manifest (e.g. ……Microsoft.VC90.CRTMicrosoft.VC90.CRT.manifest). Build as usual.

  • Your release build fails.

If you have done a successful build and then changed your optimization flags the build might fail due to MSVC crashing (an internal error has occurred blah blah blah). This is because some flag combinations just don’t seem to work, for example, having no instructions set (no sse or sse2) and fp:fast set, MSVC will crash.

  • I already downloaded the full source, and downloading the source again everytime I want to build again takes forever.

You can always just update the source by right clicking on your source folder and selecting SVN Update.

  • When making a debug build, the build fails due to an OpenEXR related error.

When making the project files using CMake, I’ve found that turning off ffmpeg, then building the project files resolves this one.

  • When making a debug build, the build fails due to a LIBC error.

No problem, add libc.lib to the ignored libs of the project by going to the blender project properties>Linker>Input>IgnoreSpecificLibrary and add libc.lib. (Thanks to Pierric Gimmig for figuring this one out). When editing a linker attribute like this you don’t have to re-compile the source again (taking forever). Just right click the blender project and only rebuild that project. That will re-link with the new settings and create your blender.exe.

  • When making a debug build, the build fails due to a python error.

The current main CMakeList file (blender/CMakeLists.txt) has a list of the libraries to be used on Windows. It has python, but not the debug python dlls as the libs to be used. To get around this, change the CMakeList.txt line “SET(PYTHON_LIB python2x)” to “SET(PYTHON_LIB python2x_d).” Re-create the project files with CMake, and it will use the debug libs. OR you could go into the blender project file properties>Linker>Input>Additional Dependencies and change the python25.lib to use python25_d.lib. When editing a linker attribute like this you don’t have to re-compile the source again (taking forever). Just right click the blender project and only rebuild that project. That will re-link with the new settings and create your blender.exe. But I find it a better solution to use the CMake way.

  • If the above setup instructions still result in a build error that is related to the SDK and DirectX files not being found (e.g. studio.h errors), here is a work-around:

Copy the Lib, Bin, and Include folders from the Windows SDK install folder into a folder called PlatformSDK in the Visual C++ directory (you will need to create this folder). I then copy the files from the Lib, Include, and the x86 folder of the Runtime (aka Bin) folders from the DirectX SDK into the PlatformSDK folder. See below:

DirectX and Windows SDK files in %Program Files%Microsoft Visual Studio 9VCPlatformSDKLib

DirectX and Windows SDK files in %Program Files%Microsoft Visual Studio 9VCPlatformSDKInclude

DirectX and Windows SDK files in %Program Files%Microsoft Visual Studio 9VCPlatformSDKBin

%d bloggers like this: