title: Using Tensorflow C++ in Android with CMake
body: <p>I have Android app with C++ code linked with CMake. I need to load and run tensorflow graph in C++ side.
  I built Tensorflow by using ./tensorflow/contrib/makefile/build_all_android.sh script.
  I added tensorflow header files and libraries into CMake file in the following way:</p>
  
  <pre><code># other libraries here
  add_library(lib_tf STATIC IMPORTED )
  set_target_properties(lib_tf PROPERTIES IMPORTED_LOCATION
      /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a)
  
  add_library(lib_proto STATIC IMPORTED )
  set_target_properties(lib_proto PROPERTIES IMPORTED_LOCATION
      /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/lib/libprotobuf.a)
  
  add_library(lib_proto_lite STATIC IMPORTED )
  set_target_properties(lib_proto_lite PROPERTIES IMPORTED_LOCATION
      /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/lib/libprotobuf-lite.a)
  
  add_library(lib_protoc STATIC IMPORTED )
  set_target_properties(lib_protoc PROPERTIES IMPORTED_LOCATION
      /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/lib/libprotoc.a)
  
  target_include_directories(pitools PRIVATE
                            # Other header paths here
                             ../../../../../../tensorflow/
                             ../../../../../../tensorflow/tensorflow/contrib/makefile/downloads
                             ../../../../../../tensorflow/tensorflow/contrib/makefile/downloads/eigen
                             ../../../../../../tensorflow/tensorflow/contrib/makefile/downloads/protobuf/src
                             ../../../../../../tensorflow/contrib/makefile/gen/proto
                             ../../../../../../tensorflow/tensorflow/contrib/makefile/downloads/nsync/public/
                             ../../../../../../tensorflow/tensorflow/contrib/makefile/gen/proto/
  )
  
  target_link_libraries( # Specifies the target library.
                         #other libs here
                         lib_tf
                         lib_proto
                         lib_proto_lite
                         lib_protoc
  )
  </code></pre>
  
  <p>Currently I just tried to create NewSession and load the graph. The code is simple for now:
  Included this headers.</p>
  
  <pre><code>#include "tensorflow/core/framework/tensor.h"
  #include "tensorflow/core/public/session.h"
  #include "tensorflow/core/util/stat_summarizer.h"
  </code></pre>
  
  <p>And created Session like this:</p>
  
  <pre><code>tensorflow::SessionOptions options;
  options.config.set_inter_op_parallelism_threads(0);
  options.config.set_use_per_session_threads(false);
  options.config.set_intra_op_parallelism_threads(0);
  options.config.set_allow_soft_placement(false);
  options.config.set_log_device_placement(false);
  tensorflow::Status status = tensorflow::NewSession(options, &amp;_session);
  </code></pre>
  
  <p>When I try to make project in Android Studio it gives me linker error like this:</p>
  
  <pre><code>/home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function std::string::_Rep::_M_dispose(std::allocator&lt;char&gt; const&amp;) [clone .part.1]: error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator&lt;char&gt; const&amp;)'
  /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function void google::protobuf::internal::RepeatedPtrFieldBase::Clear&lt;google::protobuf::RepeatedPtrField&lt;std::string&gt;::TypeHandler&gt;() [clone .isra.161]: error: undefined reference to 'std::string::_M_mutate(unsigned int, unsigned int, unsigned int)'
  /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function void google::protobuf::internal::RepeatedPtrFieldBase::Destroy&lt;google::protobuf::RepeatedPtrField&lt;std::string&gt;::TypeHandler&gt;() [clone .isra.160]: error: undefined reference to 'std::string::_Rep::_M_destroy(std::allocator&lt;char&gt; const&amp;)'
  /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function void google::protobuf::internal::RepeatedPtrFieldBase::Destroy&lt;google::protobuf::RepeatedPtrField&lt;std::string&gt;::TypeHandler&gt;() [clone .isra.160]: error: undefined reference to 'std::string::_Rep::_S_empty_rep_storage'
  /home/hhov/github/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(config.pb.o):config.pb.cc:function protobuf_tensorflow_2fcore_2fprotobuf_2fconfig_2eproto::protobuf_AssignDescriptors(): error: undefined reference to 'std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;)'
  </code></pre>
  
  <p>Find the full version of output as build_output.txt in the google drive bellow
  I also tried to built static libtensorflow_inference.so and link it to project, but I get error when tried to built it with the following command (build output is in drive also as tensorflow_build_inference_output.txt):</p>
  
  <pre><code>bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so \
  --crosstool_top=//external:android/crosstool \
  --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
  --cpu=armeabi-v7a
  --verbose_failures
  </code></pre>
  
  <p>The error of above command is:</p>
  
  <pre><code>ERROR: /home/hhov/.cache/bazel/_bazel_hhov/7cd939709f31bed648236b7a888c061b/external/double_conversion/BUILD.bazel:7:1: C++ compilation of rule '@double_conversion//:double-conversion' failed (Exit 1): false failed: error executing command 
    (cd /home/hhov/.cache/bazel/_bazel_hhov/7cd939709f31bed648236b7a888c061b/execroot/org_tensorflow &amp;&amp; \
    exec env - \
      PWD=/proc/self/cwd \
      PYTHON_BIN_PATH=/usr/bin/python \
      PYTHON_LIB_PATH=/usr/local/lib/python2.7/dist-packages \
      TF_DOWNLOAD_CLANG=0 \
      TF_NEED_CUDA=0 \
      TF_NEED_OPENCL_SYCL=0 \
    /bin/false -MD -MF bazel-out/armeabi-v7a-opt/bin/external/double_conversion/_objs/double-conversion/external/double_conversion/double-conversion/diy-fp.pic.d '-frandom-seed=bazel-out/armeabi-v7a-opt/bin/external/double_conversion/_objs/double-conversion/external/double_conversion/double-conversion/diy-fp.pic.o' -fPIC -iquote external/double_conversion -iquote bazel-out/armeabi-v7a-opt/genfiles/external/double_conversion -iquote external/bazel_tools -iquote bazel-out/armeabi-v7a-opt/genfiles/external/bazel_tools -isystem external/double_conversion -isystem bazel-out/armeabi-v7a-opt/genfiles/external/double_conversion -isystem bazel-out/armeabi-v7a-opt/bin/external/double_conversion -c external/double_conversion/double-conversion/diy-fp.cc -o bazel-out/armeabi-v7a-opt/bin/external/double_conversion/_objs/double-conversion/external/double_conversion/double-conversion/diy-fp.pic.o)
  Target //tensorflow/contrib/android:libtensorflow_inference.so failed to build
  </code></pre>
  
  <p>Google Drive link for full error messages:
  <a href="https://drive.google.com/drive/folders/13XYQveBX1cB_jH88XWlpoycEtgTswOcA?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/drive/folders/13XYQveBX1cB_jH88XWlpoycEtgTswOcA?usp=sharing</a></p>
  
  <p>Can you please say what am I doing wrong or if this is not right way to do then can you provide some steps how to do that?</p>
  
  <p>Thanks,
  hhov</p>