How to build Tensorflow on Windows with /MT (CPU & GPU)

Building Tensorflow for Windows may be a pain in the neck even if you follow the original documentation. It gets worse when you have to change the type of runtime library your build must output. In this tutorial, we will learn how to change the original Tensorflow build (v1.4) to produce a static library (/MT) instead of a DLL (/MD).

Arnaldo Gualberto
6 min readApr 14, 2018

UPDATE: I recently have built Tensorflow v1.4 with GPU and updated instructions in my repo.

A few months ago, I had trained a face detector using the Tensorflow framework in Python. Once I finished tuning my network, I had to deploy it in production inside a C++ application. Since Tensorflow is originally written in C++, I thought that would be easy. I am so naive…

Following the original instructions in Tensorflow’s repository, I was able to build it from source and add it to an empty project in Visual Studio. However, when I tried to integrate it into the Visual Studio project it was supposed to work, something went wrong!? The error:

error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in file.obj
That was exactly my reaction

In this tutorial, we’ll start understanding the differences between the /MD and /MT. Later, we will see the whole list of software required to build Tensorflow (v1.4) on Windows with /MT and how to setup everything we need to. Finally, I will guide you through a step-by-step guide to build and add Tensorflow into an empty Visual Studio project.

What’s the difference between /MD and /MT?

In basic words, they are mutually exclusive options we pass to the compiler to indicate how the libraries will be linked at runtime.

If you are a not a programmer or you are not used to configure/build projects, this definition may be just a bunch of technical words for you. So, let’s give a practical example:

Imagine you have an application built with /MD option and you want to share it with a friend or deploy it in production. In this case, you must pack your application with all the DLLs it depends on. Although the executable may be smaller (since it doesn’t have the library embedded in it), your application is exposed to system updates and you have to ensure the user has the right DLL versions onto their machine for running your program.

On the other hand, when we use /MT, your executable won’t depend on a DLL being present on the target system. All the runtime libraries will be “inside it”. Thus, you can ignore the whole DLL mess. However, a /MT application is much more prone to cause conflicts than /MD. In general, most of the application for windows are build using /MD by default — as well as Tensorflow.

1. Setup

Here is the full list of software I used to build Tensorflow from source:

If you want to build with CUDA, you’ll also need to download and install the following:

Obs.: as reported by this issue, Tensorflow v1.4 does not work with CUDA 9.

Note: You must add all the software above to your System Path (%PATH% environment variables) before proceeding.

Before we start, Tensorflow also requires Python 3.5 and Numpy 1.11.0 or later. To achieve this, we are going to create a virtual environment. Thus, open terminal and type:

$ conda create -n tfbuild python=3.5 numpy

Once it finished, be sure to take note of python executable (python.exe) and python library (python35.lib) just installed. In my case, they are in the following directories:

C:/Users/X/AppData/Local/Continuum/miniconda3/envs/tfbuild/ C:/Users/X/AppData/Local/Continuum/miniconda3/envs/tfbuild/libs/

2. Step-by-step build

In order to build Tensorflow, make sure you have at least 12 GB of RAM memory.

  1. First of all, clone/download this Tensorflow Fork and this repo into the root of your C:\ hard disk.
  2. Copy the contents of C:/tensorflow-build-mt/v1.4/build to C:/tensorflow/build
  3. Copy the contents of C:/tensorflow-build-mt/v1.4/cmake to C:/tensorflow/tensorflow/contrib/cmake

Continue with the instructions below according to your needs:

Build for CPU

4. Open the file C:/tensorflow/build/my-build.bat in a text editor and edit the variables CMAKE_EXE, SWIG_EXE, PY_EXE, PY_LIB and MSBUILD_EXE according to your installations, if needed.

5. Open Windows Start Menu, type “x64” and choose the “x64 Native Tools Command Prompt for VS 2017”. With the terminal opened, type:

$ cd C:\tensorflow\build
$ my-build.bat

6. There is a bug in Tensorflow 1.4 tf_stream_executor.vcxproj (as referenced by this video). So, open C:/tensorflow/build/tf_stream_executor.vcxproj in a text editor and add the path C:\tensorflow\third_party\toolchains\gpus\cuda to the additional include directories in configuration Release x64section, as in the picture below:

Fixing the tf_stream_executor.vcxproj error

7. Invoke MSBuild to build TensorFlow:

$ %MSBUILD_EXE% /p:Configuration=Release /p:Platform=x64 /m:6 tensorflow.sln /t:Clean;Build /p:PreferredToolArchitecture=x64

Build for GPU

4. Open the file C:/tensorflow/build/my-build-cuda.bat in a text editor and edit the variables CMAKE_EXE, SWIG_EXE, PY_EXE, PY_LIB, CUDNN_HOME, CUDA_TOOLKIT_ROOT_DIR, and MSBUILD_EXE according to your installations, if needed.

5. Open Windows Start Menu, type “x64” and choose the “x64 Native Tools Command Prompt for VS 2017”. With the terminal opened, type:

$ cd C:\tensorflow\build
$ my-build-cuda.bat

3. Visual Studio Project

  1. Open Visual Studio 2017, click on File ➡️ New ➡️ Project and under Visual C++/General, choose the Empty Project template. I will name it TensorflowTest. Save it wherever you want.
  2. Change the Solution Configuration to Release, and the Solution Platform to x64:

3. Add a new .cpp file (main.cpp) and paste the code below:

50+ lines of code to multiply two 2-by-2 matrices 🙊

4. Right-click on your project and choose Properties. Go to C/C++ ➡️ Code Generation and change the Runtime Libraryoption to Multi-Threaded (/MT).

The main trick to linking to the C++ Tensorflow library is getting all the project properties right. First, we need to show the compiler where are all the appropriate Tensorflow header files. Therefore, right click on your project and choose Properties. Now, go to C/C++ ➡️ General ➡️ Additional Include Directories and add the following directories:

The next step is to tell the Linker where are the libraries we need to run our example. Thus, open again the project properties and go to Linker ➡️ General ➡️ Additional Libraries Directories.

Finally, we need to tell the Linker what are such libraries. Now, go to Project Properties ➡️ Linker ➡️ Input ➡️ Additional Dependencies and add the following dependencies:

If you’ve done everything right, you are now able to build our TensorflowTest project. Just right-click on the project and choose build.

That's how I feel!

If I helped you through this tough process somehow, please share it and leave some claps 😉

Final Words

I created a Github Repository where I will keep instructions to build specific versions of Tensorflow with /MT. I intend to have a build for every stable version of Tensorflow. Please, open an issue if you find any trouble about building a specific version or open a pull request if you have already built any Tensorflow version from source (with /MT option, of course) which is not in this repo.

Does it work for newer versions of Tensorflow?

I will try to build Tensorflow 1.7 or newer as long as its build for Windows passes.

References

--

--