Build TensorFlow C++ Library from source

In this guide, we will demonstrate how to build TensorFlow C++ library from source so that you can use TensorFlow C++ API. This is needed as TensorFlow has no documentation on how to build C++ Library from source and provided no pre-build C++ library.

When we will build TensorFlow C++ library, two shared object (.so) files are created as follows:

  • libtensorflow_cc.so
  • libtensorflow_framework.so

The steps to build TensorFlow C++ Library are as follows:
Method 1: Naive build

  • Step 1.1: Clone TensorFlow source code
  • Step 1.2: Bazel build for libtensorflow_cc
  • Step 1.3: Bazel build for headers
  • Step 1.4: Find the files
    Method 2: with optimizations
  • Step 2.1: Clone TensorFlow source code
  • Step 2.2: Bazel build for libtensorflow_cc
  • Step 2.3: Test the bazel build
  • Step 2.4: Bazel build for libtensorflow_framework

Method 1: Naive build

In this method, we build TensorFlow C++ using the default options.

Step 1.1: Clone TensorFlow source code

Get the source code of TensorFlow from GitHub and change to the version you need to build for.

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout v2.0

Step 2: Bazel build for libtensorflow_cc

To build the library libtensorflow_cc.so, use the following command:

bazel build tensorflow:tensorflow_cc

Step 3: Bazel build for headers

To build the header files, use the following command:

bazel build tensorflow:install_headers

Step 4: Find the files

At this point, all files are ready and are available at this location:

tensorflow/bazel-bin/tensorflow

The main files are:

  • The shared object files .so (like libtensorflow_cc.so and libtensorflow_framework.so)
  • The header files (in tensorflow/bazel-bin/tensorflow/include like cc and core).

Method 2: with optimizations

In this method, you can specify optimizations native to your system (like to use AVX2) and get an optimized build of TF C++.

Step 2.1: Clone TensorFlow source code

Get the source code of TensorFlow from GitHub and change to the version you need to build for.

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout v2.0

Step 2.2: Bazel build for libtensorflow_cc

To build the library libtensorflow_cc.so, use the following command:

bazel build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --copt="-O3" -c opt 
            --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.1 
            --copt=-msse4.2 --copt=-mfpmath=both --show_result 0 
            --noshow_progress //tensorflow:libtensorflow_cc.so

Step 2.3: Test the bazel build

To test the Bazel build, use the following command:

bazel test --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --copt="-O3" -c opt 
           --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.1 
           --copt=-msse4.2 --copt=-mfpmath=both --show_result 0 
           --noshow_progress //tensorflow/tools/lib_package:libtensorflow_test

Step 2.4: Bazel build for libtensorflow_framework

To build libtensorflow_framework, use the following command:

bazel build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --copt="-O3" -c opt 
            --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.1 
            --copt=-msse4.2 --copt=-mfpmath=both --show_result 0 
            --noshow_progress //tensorflow/tools/lib_package:libtensorflow

Check the files

Go to the folder where the generated build files are placed:

cd bazel-genfiles/tensorflow

Check the files that are present:

ls

_api      __init__.py                          
libtensorflow_framework.so.1                
stream_executor
c         libtensorflow_cc.so                  
libtensorflow_framework.so.2.0.0           
tools
cc        libtensorflow_cc.so.1                
libtensorflow_framework.so.2.0.0-2.params  
virtual_root.__init__.py
compiler  libtensorflow_cc.so.2.0.0           
libtensorflow.so.2.0.0-2.params
contrib   libtensorflow_cc.so.2.0.0-2.params  
lite
core      libtensorflow_framework.so           python

Note, we have folders like:

  • cc
  • c
  • core

These folders have header files which you can use in your C++ code.

Note the .so files are also present:

  • libtensorflow_framework.so
  • libtensorflow_cc.so

You need to set the LD_LIBRARY_PATH to this folder so that your C++ code can find the TensorFlow C++ library.

Note that TensorFlow C++ APIs are unstable and TensorFlow team does not recommend using them and does not validate them across TensorFlow releases. There are cases when using TensorFlow C++ API is a must and hence, you need to build it on your own and use it.

With this article at OpenGenus, you must have the complete idea of building TensorFlow C++ from source.