TensorFlow’s Python interface is popular for its convenience and usefulness, but in practice we may need interfaces from other programming languages. This article will show you how to compile the C/C++ interface for TensorFlow.

Installation environment: Ubuntu 16.04 Python 3.5 CUDA 9.0 cuDNN 7 Bazel 0.17.2 TensorFlow 1.11.0

1. Install Bazel

  • Sudo apt-get install openJDK-8-jdk

  • Add the Bazel software source

echo "Deb [arch = amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
Copy the code
  • Install Bazel sudo apt-get update && sudo apt-get install Bazel

  • Check out the official Installation guide for Bazel here

2. Compile the TensorFlow library

  • Click here to download the TensorFlow source

  • Go to the source directory and run./configure to configure. Build from source – View sample Configuration Session Settings, including Python paths, CUDA and CUDNN versions and paths, and graphics card computing power. The following is my configuration process for reference only.

You have bazel 0.17.2 installed.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3.5


Found possible Python library paths:
  /usr/local/lib/python3.5/dist-packages
  /usr/lib/python3/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python3.5/dist- Packages] Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: n No Apache Ignite support will be enabledfor TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: y
CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 


Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 


Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 


Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 


Do you wish to build TensorFlow with TensorRT support? [y/N]: n
No TensorRT support will be enabled forTensorFlow. Please specify the locally installed NCCL version you want to use. [Default is to use https://github.com/nvidia/nccl]: Please specify a list of comma-separated Cuda compute capabilities you want to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Please note that each additional compute capability significantly increases your [Default is: 6.1]: Do you want to use clang as CUDA compiler? [Y /N]: n nvcc will be used as CUDA compiler. Please specifywhich gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 


Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n
Not configuring the WORKSPACE for Android builds.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
	--config=mkl         	# Build with MKL support.
	--config=monolithic  	# Config for mostly static monolithic build.
	--config=gdr         	# Build with GDR support.
	--config=verbs       	# Build with libverbs support.
	--config=ngraph      	# Build with Intel nGraph support.
Configuration finished
Copy the code
  • Go to the tensorflow directory and compile. After the compilation is successful, the libtensorflow_cc.so file is displayed in the /bazel-bin/tensorflow directory
C: bazel build :libtensorflow.so C: bazel build :libtensorflow_cc.soCopy the code

3. Compile other dependencies

  • Enter tensorflow/contrib/makefile directory, run. / build_all_linux. Sh, success will appear after a gen folder

  • /autogen.sh: 4: autoreconf: not found sudo apt-get install autoconf automake libtool

4. Test

  • Cmaklist.txt
Cmake_minimum_required (VERSION 3.8) project (Tensorflow_test)set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)


include_directories(
        /media/lab/data/yongsen/tensorflow-master
        /media/lab/data/yongsen/tensorflow-master/tensorflow/bazel-genfiles
        /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/gen/protobuf/include
        /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/gen/host_obj
        /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/gen/proto
        /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/downloads/nsync/public
        /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/downloads/eigen
        /media/lab/data/yongsen/tensorflow-master/bazel-out/local_linux-py3-opt/genfiles
        /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/downloads/absl
)

add_executable(Tensorflow_test ${SOURCE_FILES})

target_link_libraries(Tensorflow_test
        /media/lab/data/yongsen/tensorflow-master/bazel-bin/tensorflow/libtensorflow_cc.so
        /media/lab/data/yongsen/tensorflow-master/bazel-bin/tensorflow/libtensorflow_framework.so
        )
Copy the code
  • Create a reply
#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>
#include <iostream>

using namespace std;
using namespace tensorflow;

int main(a)
{
    Session* session;
    Status status = NewSession(SessionOptions(), &session);
    if(! status.ok()) {cout << status.ToString() << "\n";
        return 1;
    }
    cout << "Session successfully created.\n";
    return 0;
}
Copy the code
  • View the TensorFlow version
#include <iostream>
#include <tensorflow/c/c_api.h>

int main(a) {
   std: :cout << "Hello from TensorFlow C library version" << TF_Version();
    return 0;
}

TensorFlow C library version1.11.0-rc1
Copy the code
  • If some header files are missing, search for the path in the tensorflow root directory and add it to the Cmakelist.

For more highlights, watch seniusen!