1
votes

I am trying to build Tensorflow 1.4.0 from sources for C/C++ in 64-bit Ubuntu 17.10. I tried to follow the instructions in Tensorflow website and other website for C++ using Bazel and summarize them as follows:

cd tensorflow
./configure
(select N for all the options)
bazel build --config=opt --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --local_resources 2048,.5,1.0 //tensorflow:libtensorflow_cc.so //tensorflow:libtensorflow_framework.so //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
sudo -H pip3 install /tmp/tensorflow_pkg/tensorflow-1.4.0rc1-cp36-cp36m-linux_x86_64.whl

When I tried to build a simple test program and include tensorflow/core/public/session.h in Eclipse (with the include path set to the tensorflow root folder downloaded from git), it complained several things:

  1. Missing Google's protobuf common.h in device_attributes.pb.h (I cannot use apt-get to install protobuf as it will complain version too old - lower than 3.0.0.4 check in device_attributes.pb.h). I downloaded the latest protobuf and follow the instructions to install it
  2. Missing Eigen3's Tensor file in tensor.h, where tensor.h includes Tensor file in [tensorflow directory]/third_party/eigen3/unsupported/Eigen/CXX11/Tensor, and [tensorflow directory]/third_party/eigen3/unsupported/Eigen/CXX11/Tensor includes the missing [/usr/include/eigen3/]unsupported/Eigen/CXX11/Tensor file provided by libeigen3-dev. I apt-get to install libeigen3-dev.
  3. After I installed libeigen3-dev, I added /usr/include/eigen3 to the Eclipse's include path as the Tensor file in tensorflow directory is unable to find the Tensor file in /usr/include (with extra eigen3 directory).
  4. Missing nsync_cv.h file in mutex.h. I added nsync folder in tensorflow directory to the include path.

In summary, I have added the following paths to the include path in Eclipse:

  • [tensorflow directory]/
  • [tensorflow directory]/bazel-genfiles
  • [tensorflow directory]/bazel-tensorflow/external/nsync/public
  • /usr/include/eigen3

I added the library path [tensorflow directory]/bazel-bin/tensorflow with the following libraries:

  • tensorflow
  • tensorflow_cc
  • tensorflow_framework

I have also added -std=c++11 to the GCC C++ compiler and GCC C++ Linker in the Eclipse's project settings. In the end it encountered a linker error:

[tensorflow directory]/tensorflow/core/platform/default/logging.h:187: undefined reference to `tensorflow::internal::CheckOpMessageBuilder::NewString[abi::cxx11]()'

I have seen a thread discussing similar issue but I did set -D_GLIBCXX_USE_CXX11_ABI=0 and Ubuntu 17.10 is using gcc5 or above.

Please kindly suggest how to fix the linker error. Many thanks!

2

2 Answers

1
votes

Officially, you have to create your C++ project in tensorflow sourcetree, write BUILD file and compile it using bazel. Look at this.

I wrote a blog post: https://tuanphuc.github.io/standalone-tensorflow-cpp/ giving detailed instructions to make a standalone C++ Tensorflow with configuration: - Ubuntu 17.10 - gcc 7.2.0 - tensorflow 1.4.0 - Python 2 or 3 - cmake 3.9.6 - Eigen 3.3.4 - Protobuf (master branch) - Googletest (master branch) - bazel Hope it helps

References:

Packaged TensorFlow C++ library for bazel-independent use

graph.pb.h missing

1
votes

If you don't want to tinker with your environment too much there are two unofficial alternatives. One is to compile the C++ API with Floop's tensorflow_cc project and install on your system. The other possibility is to install one of the releases of my packaging project for C and C++ API of Tensorflow. Both projects use CMake (instead of Bazel) to support C++ compilation of your source files.

From Tensorflow point of view, the advantage of tensorflow_cc that you can build GPU support if you want while my project can only use CPU for inference.