Skip to content
gabrigarjim edited this page Sep 9, 2020 · 4 revisions

Stand-Alone Tensorflow C++ building with CMake

Prerequisites

To build Tensorflow you will need the following items:

  • CMake.
  • Xcode , with command line tools installed.
  • Bazel.

Tested Systems and Software versions

  • TensorFlow: Release 2.0
  • CMake: 3.15.3
  • Bazel: 0.25.0
  • Xcode: 10.3
  • MacOS: Mojave (10.14.5)

Linux build has not been tested, but it seems it will work too.

Building the C++ shared library

Take the TensorFlow source from GitHub and clone in your local directory :

git clone -b <name_of_the_branch> https://github.com/tensorflow/tensorflow.git

Configure the building by executing the script:

./configure

The script will ask you for several options, like python paths and GPU buildings with CUDA. Be sure to set all this options according to your requirements, and check that all paths are set properly. If you are unsure about the y/N questions, the default configuration works well (all /N).

Then run the following commands from the root of the TensorFlow repository to build the shared libraries: (Warning: this may take several hours to finish)

bazel build tensorflow:libtensorflow_cc.so
bazel build tensorflow:libtensorflow_framework.so

Now copy them to your library directory:
sudo cp bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib
cp bazel-bin/tensorflow/libtensorflow_framework.so /usr/local/lib

and be sure of copying the Alias files as well. This will avoid troubles related with TensorFlow versions at run time of your project binaries.
cp bazel-genfiles/tensorflow/Name_of_the_Alias_ /usr/local/lib\

Create an include directory and Copy all headers needed there:

sudo mkdir -p /usr/local/include/google/tensorflow
sudo cp -r tensorflow /usr/local/include/google/tensorflow/

cp bazel-genfiles/tensorflow/core/framework/*.h /usr/local/include/google/tensorflow/tensorflow/core/framework\

cp bazel-genfiles/tensorflow/core/kernels/*.h /usr/local/include/google/tensorflow/tensorflow/core/kernels\

cp bazel-genfiles/tensorflow/core/lib/core/*.h /usr/local/include/google/tensorflow/tensorflow/core/lib/core\

cp bazel-genfiles/tensorflow/core/protobuf/*.h /usr/local/include/google/tensorflow/tensorflow/core/protobuf\

cp bazel-genfiles/tensorflow/core/util/*.h /usr/local/include/google/tensorflow/tensorflow/core/util\

cp bazel-genfiles/tensorflow/cc/ops/*.h /usr/local/include/google/tensorflow/tensorflow/cc/ops\

Building some tools from inside the Tensorflow repository

This step builds Protobuf and Eigen as well. Although you can point to your own installation of both, you need some tools that are built here, so this step is not optional. From your TensorFlow root, make:

./tensorflow/contrib/makefile/download_dependencies.sh

and copy the downloads folder to your /usr/local/include/google/tensorflow/tensorflow/contrib/makefile folder.

Building with CMake

You can build now TensorFlow with CMake in anyway you want, just be sure to include directories to all libraries needed in your code. Some relevant paths are

  • TENSORFLOW_PATH (/usr/local/include/google/tensorflow)
  • TENSORFLOW_PATH/tensorflow/contrib/makefile/downloads/absl
  • EIGEN_PATH
  • PROTOBUF_PATH

for headers, and

  • /usr/local/lib (or your custom path)

as the path used by your compiler to link the binary with the .so libraries you have built with Bazel. Link your target with the two shared libraries, an avoid problems with unrecognized symbols by setting the standard 11 for C++ .