14

I'm interested in incorporating TensorFlow into a C++ server application built in Visual Studio on Windows 10 and I need to know if that's possible. Google recently announced Windows support for TensorFlow: https://developers.googleblog.com/2016/11/tensorflow-0-12-adds-support-for-windows.html but from what I can tell this is just a pip install for the more commonly used Python package, and to use the C++ API you need to build the repo from source yourself: How to build and use Google TensorFlow C++ api I tried building the project myself using bazel, but ran into issues trying to configure the build.

Is there a way to get TensorFlow C++ to work in native Windows (not using Docker or the new Windows 10 Linux subsystem, as I've seen others post about)?

Thanks,

Ian

Community
  • 1
  • 1
Ian Conway
  • 367
  • 1
  • 2
  • 13

5 Answers5

13

It is certainly possible to use TensorFlow's C++ API on Windows, but it is not currently very easy. Right now, the easiest way to build against the C++ API on Windows would be to build with CMake, and adapt the CMake rules for the tf_tutorials_example_trainer project (see the source code here). Building with CMake will give you a Visual Studio project in which you can implement your C++ TensorFlow program.

Note that the tf_tutorials_example_trainer project builds a Console Application that statically links all of the TensorFlow runtime into your program. At present we have not written the necessary rules to create a reusable TensorFlow DLL, although this would be technially possible: for example, the Python extension is a DLL that includes the runtime, but does not export the necessary symbols to use TensorFlow's C or C++ APIs directly.

mrry
  • 125,488
  • 26
  • 399
  • 400
  • For someone like me who hasn't used CMake before, how do you use it to build `tf_tutorials_example_trainer`? – HelloGoodbye Apr 13 '17 at 15:58
  • 1
    After following the [instructions to run `cmake`](https://github.com/tensorflow/tensorflow/blob/395cfc42ee3c5842f5383f4049674c012998b133/tensorflow/contrib/cmake/README.md) and generate a set of Visual C++ project files, you should be able to run `MSBuild /p:Configuration=Release tf_tutorials_example_trainer.vcxproj` in the build directory (or load `tf_tutorials_example_trainer.vcxproj` into Visual Studio and build it from there). – mrry Apr 13 '17 at 16:37
  • Thanks. I am attempting to follow the guide, but am experiencing [some problems](http://stackoverflow.com/questions/43940883/errors-when-building-tensorflow-with-cmake-on-windows-10). – HelloGoodbye May 12 '17 at 15:04
  • Hm, it seems like [`tensorflow\tensorflow\core\kernels\cuda_solvers.h` is looking for cusolverDn.h in the wrong folder](http://stackoverflow.com/questions/43998199/why-is-tensorflow-looking-for-cusolverdn-h-in-cuda-include). – HelloGoodbye May 16 '17 at 09:58
  • Did you manage to build it? If so, can you share the steps (probably as a solution), as I am also trying to build the files using VS for my project. Thanks! – programmer May 30 '17 at 00:58
  • The CMake build instructions on GitHub [here](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/cmake/README.md) worked perfectly for me on Win 7 with Visual Studio 2015. Note that they use MSBuild, not Visual Studio directly. I found it impossible to invoke the 64-bit compiler through Visual Studio, and the 32-bit compiler gave me heap space errors. MSBuild ran perfectly first time. – omatai Feb 20 '18 at 20:58
6

There is a detailed guide by Joe Antognini and a similar TensorFlow ReadMe at GitHub explaining the building of TensorFlow source via CMake. You also need to have SWIG installed on your machine which allows connecting C/C++ source with the Python scripting language. I did use Visual CMAKE (cmake-gui) with the screen capture shown below.

cmake-gui setup (with SWIG) for building TensorFlow C++ source with Visual Studio

In the CMake configuration, I used Visual Studio 15 2017 compiler. Once this stage successfully completes, you can click on the Generate button to go ahead with the actual build process.

However, on Visual Studio 2015, when I attempted building via the "ALL_BUILD" project, the setup gave me "build tools for v141 cannot be found" error. This did not go away even when I attempted to retarget my solution. Finally, the solution got built successfully with Visual Studio 2017. You also need to manually set the SWIG_EXECUTABLE path in CMake before it successfully configures.

As indicated in the Antognini link, for me the build took about half an hour on a 16GB RAM, Core i7 machine. Once done, you might want to validate your build by attempting to run the tf_tutorials_example_trainer.exe file.

Hope this helps!

Teh Sunn Liu
  • 517
  • 3
  • 16
Psi-Ed
  • 683
  • 1
  • 9
  • 22
  • 2
    Joe Antognini's post seemed like a useful set of instructions, but ended up wasting me hours spread over days because it is very easy to not realise you are supposed to be using the 64-bit MSVC compiler, and very hard to do so from within Visual Studio. By comparison, when I bypassed Visual Studio and used MSBuild as per "official" CMake build instructions, it worked immediately. – omatai Feb 20 '18 at 20:40
3

For our latest work on building TensorFlow C++ API on Windows, please look at this github page. This works on Windows 10, currently without CUDA support (only CPU).

PS: Only the bazel build method works, because CMake is not supported and not maintained anymore, resulting in CMake configuration errors.

srood
  • 43
  • 6
1

I had to use a downgraded version of my Visual Studio 2017 (from 15.7.5 to 15.4) by adding "VC++ 2017 version 15.4 v14.11 toolset" through the installer (Individual Components tab).

The cmake command which worked for me was:

cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release ^
-T "v141,version=14.11" ^
-DSWIG_EXECUTABLE="C:/Program Files/swigwin-3.0.12/swig.exe" ^
-DPYTHON_EXECUTABLE="C:/Program Files/Python/python.exe" ^
-DPYTHON_LIBRARIES="C:/Program Files/Python/libs/python27.lib" ^
-Dtensorflow_ENABLE_GPU=ON ^
-DCUDNN_HOME="C:/Program Files/cudnn-9.2-windows10-x64-v7.1/cuda"  ^
-DCUDA_TOOLKIT_ROOT_DIR="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0"

After the build, open tensorflow.sln in Visual Studio and build ALL_BUILD.

If you want to enable GPU computation, do check your Graphics Card here (Compute Capability > 3.5). Do remember to install all the packages (Cuda Toolkit 9.0, cuDNN, Python 3.7, SWIG, Git, CMake...) and add the paths to the environment variable in the beginning.

Chong Lip Phang
  • 8,755
  • 5
  • 65
  • 100
0

I made a README detailing how to I built the Tensorflow dll and .lib file for the C++ API on Windows with GPU support building from source with Bazel. Tensorflow version 1.14

The tutorial is step by step and starts at the very beginning, so you may have to scroll down past steps you have already done, like checking your hardware, installing Bazel etc. Here is the url: https://github.com/sitting-duck/stuff/tree/master/ai/tensorflow/build_tensorflow_1.14_source_for_Windows

Probably you will want to scroll all the way down to this part: https://github.com/sitting-duck/stuff/tree/master/ai/tensorflow/build_tensorflow_1.14_source_for_Windows#step-7-build-the-dll

It shows how to pass command to create .lib and .dll.

Then to test your .lib you should link it into your c++ project,

Then it will show you how to identify and fix the missing symbols using the TF_EXPORT macro

I am actively working on making this tutorial better so feel free to leave comments on this answer if you are having problems.

sitting-duck
  • 961
  • 1
  • 10
  • 22