7

I'm trying to use tensorflow as a external library in my C++ application (mainly following this tutorial). What I done so far:

  1. I have cloned the tensorflow reporitory (let's say, that the repo root dir is $TENSORFLOW)
  2. Run /.configure (which all settings default, so no CUDA, no OpenCL etc.).
  3. Build shared library with bazel build -c /opt //tensorflow:libtensorflow_cc.so (build completed successfully)
  4. Now I'm trying to #include "tensorflow/core/public/session.h". But after including it (and adding $TENSORFLOW and $TENSORFLOW/bazel-genfiles to include path), I'm receiving error:

    $TENSORFLOW/tensorflow/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:42:
    fatal error: unsupported/Eigen/CXX11/Tensor: No such file or directory
    

There is a github issue created for similar problem, but it's marked as closed without any solution provided. Also I tried with master branch as well as v.1.4.0 release.

Do you happen to know, what could cause this kind of problem and how to deal with it?

Szał Pał
  • 81
  • 2
  • 4

3 Answers3

5

I (and many others) agonized over the same problem. It probably can be solved using bazel but I don't know that tool well enough and now I solve this using make. The source of confusion is that a file named Tensor is included and it itself includes a file named Tensor, which has caused some people to wrongly conclude Tensor is including itself.

If you built and installed the python .whl file there will be a tensorflow directory in dist-packages and an include directory below that, e.g. on my system:

/usr/local/lib/python2.7/dist-packages/tensorflow/include

From the include directory

find . -type f -name 'Tensor' -print
./third_party/eigen3/unsupported/Eigen/CXX11/Tensor
./external/eigen_archive/unsupported/Eigen/CXX11/Tensor

The first one has

#include "unsupported/Eigen/CXX11/Tensor"

and the file that should satisfy this is the second one.

So to compile session.cc that includes session.h, the following will work

INC_TENS1=/usr/local/lib/python2.7/dist-packages/tensorflow/include/
INC_TENS2=${INC_TENS1}external/eigen_archive/
gcc -c -std=c++11 -I $INC_TENS1 -I $INC_TENS2 session.cc

I've seen claims that you must build apps from the tensorflow tree and you must use bazel. However, I believe all the header files you need are in dist-packages/tensorflow/include and at least for starters you can construct makefile or cmake projects.

Roger Levy
  • 61
  • 1
  • 3
  • This works perfectly fine with g++ but the same path when added to cmake throws a n error ```/usr/local/include/google/tensorflow/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:10: fatal error: 'unsupported/Eigen/CXX11/Tensor' file not found #include "unsupported/Eigen/CXX11/Tensor"``` Did you ever try this with cmake? – Isabela Aug 07 '19 at 09:10
  • This worked for me for tensorflow 1.12; although my include directory is somewhere else (depends on how it is installed). Agree with the answer, this seems to be assuming you're using bazel to build the project but I also don't have the expertise and keep running into issues with it making it difficult. I'd assume the cmake find package for tensorflow would report out these include directories? – syntheticgio Dec 02 '19 at 18:32
2

Slightly off-topic, but I had the same error with a C++ project using opencv-4.5.5 and compiled with Visual Studio (no problem with opencv-4.3.0, and no problem with MinGW).

To make it work, I had to add to my root CMakeLists.txt:

add_definitions(-DOPENCV_DISABLE_EIGEN_TENSOR_SUPPORT)

If that can help someone...

PJ127
  • 986
  • 13
  • 23
1

the problem was actually in the relative path of the header file taken in the Tensor file.

installed path for Tensor is /usr/include/eigen3/unsupported/Eigen/CXX11/Tensor

but mentioned in the Tensor file is "unsupported/Eigen/CXX11/Tensor"

So there should be an entry upto /usr/include/eigen3/ in the project path to run this correctly so that it can be used.

codeKarma
  • 117
  • 8