Skip to content

Instantly share code, notes, and snippets.

@Era-Dorta
Last active January 8, 2018 18:32
Show Gist options
  • Save Era-Dorta/a6a82b75ea8277d12829eee81d6d2203 to your computer and use it in GitHub Desktop.
Save Era-Dorta/a6a82b75ea8277d12829eee81d6d2203 to your computer and use it in GitHub Desktop.
Configuration for a Visual Studio project that depends on tensorflow.dll in C++
Follow instructions in the following link to compile tensorflow.
Build as a shared library -Dtensorflow_BUILD_SHARED_LIB=ON
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/cmake
Create new empty project in visual studio.
A good example to test as main.cpp would be
https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/examples/label_image/main.cc
The following assumes that Tensorflow was downloaded in C:\tensorflow-r1.2
************
Release
************
Include directories
C:\tensorflow-r1.2;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\external\eigen_archive;C:\tensorflow-r1.2\third_party\eigen3;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\protobuf\src\protobuf\src%(AdditionalIncludeDirectories)
Preprocessor definitions
WIN32;_WINDOWS;NDEBUG;_ITERATOR_DEBUG_LEVEL=0;EIGEN_AVOID_STL_ARRAY;NOMINMAX;_WIN32_WINNT=0x0A00;LANG_CXX11;COMPILER_MSVC;OS_WIN;_MBCS;WIN64;WIN32_LEAN_AND_MEAN;NOGDI;PLATFORM_WINDOWS;TENSORFLOW_USE_EIGEN_THREADPOOL;EIGEN_HAS_C99_MATH;%(PreprocessorDefinitions)
Additional Library Directories
C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\Release;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\protobuf\src\protobuf\Release;%(AdditionalLibraryDirectories)
Additional Dependencies
libprotobuf.lib;tensorflow.lib;%(AdditionalDependencies)
If the project compiles but it complains at run time about the tensorflow.dll, copy it to the project x64\Release folder.
************
Debug
************
Compile tensorflow static library in debug
MSBuild /p:Configuration=Debug tensorflow_static.vcxproj
Set the custom project to use the tensorflow_static.lib generated in the previous step
Include directories
C:\tensorflow-r1.2;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\external\eigen_archive;C:\tensorflow-r1.2\third_party\eigen3;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\protobuf\src\protobuf\src%(AdditionalIncludeDirectories)
Preprocessor definitions
WIN32;_WINDOWS;_ITERATOR_DEBUG_LEVEL=2;EIGEN_AVOID_STL_ARRAY;NOMINMAX;_WIN32_WINNT=0x0A00;LANG_CXX11;COMPILER_MSVC;OS_WIN;_MBCS;WIN64;WIN32_LEAN_AND_MEAN;NOGDI;PLATFORM_WINDOWS;TENSORFLOW_USE_EIGEN_THREADPOOL;EIGEN_HAS_C99_MATH;
Additional Library Directories
C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\fft2d\src\fft2d\Debug;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\farmhash\src\farmhash\Debug;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\gif\src\gif-build\Debug;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\Debug;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\grpc\src\grpc\Debug;C:\tensorflow-r1.2\tensorflow\contrib\cmake\build\protobuf\src\protobuf\Debug;
Additional Dependencies
fft2d.lib;farmhash.lib;giflib.lib;gpr.lib;grpc_unsecure.lib;grpc++_unsecure.lib;tf_protos_cc.lib;libprotobufd.lib;/WHOLEARCHIVE:tensorflow_static.lib;%(AdditionalDependencies)
@spk921
Copy link

spk921 commented Sep 21, 2017

Is this GPU support? And if so have you build tensorflow with GPU support in Windows with visual studio2015?

@helen-medina
Copy link

Hi,
I have tried to build the library for tensorflow with GPU enabled, however it raise an error about not enough space. It looks like there is an issue with the size of array or any global variable:

You can fin more details in the link below:
https://stackoverflow.com/questions/46315453/error-compiling-static-library-c-for-tensorflow-when-gpu-enabled-in-cmake?noredirect=1#comment79593898_46315453

Could you tell me how could I solve this issue?

@Era-Dorta
Copy link
Author

I haven't compiled for Windows with GPU support. I'm not sure what is the cause of the problem that you are having, but a few useful tips.

  • Compiling dlls in release mode is the better supported than the static libraries, however the debug dll for 1.2 does not work, it should be fixed in 1.3 but I haven't tested yet that version
  • Visual studio might fail to pick up the 64bit compiler option, compiling from the command line is recommended
  • Building can take a lot of memory, I don't remember the exact number, but I think some people have trouble with machines with 8GB of RAM and less

@leemgs
Copy link

leemgs commented Oct 27, 2017

Building can take a lot of memory, I don't remember the exact number, but I think some people have trouble with machines with 8GB of RAM and less

If we try to use "-Wl,--no-keep-memory" option in order to compile successfully Tensorflow with Cmake on 8GB of RAM and less system?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment