12

I've got a c++ project set up in CLion that uses CMake. I am using various 3rd party libraries and would like to also integrate Tensorflow.

I've tried bazel to compile Tensorflow to a shared library libtensorflow.so which kind of worked however there are still quite a few dependencies (e.g. to a current protobuf version and once I do that there are more) that I'd have to fix.

Is there a way to use the standard Tensorflow git repository and somehow link the libraries that are pre-compiled for python usage? Or is there another convenient way?

Tensorflow in Python works well for me.

Amelse Etomer
  • 1,253
  • 10
  • 28

2 Answers2

6

I am aware this answer is quite late, but I encountered your exact problem and was able to solve it. I created a repository here that describes how to accomplish exactly what you want. The gist is:

  • Add a build rule to the TensorFlow repository containing all of the required C++ elements.
  • Build the shared library using Bazel and copy all headers to /usr/local.
  • Install specific versions of Protobuf and Eigen (this is done automatically with scripts) or add them as external dependencies.
  • Configure your CMake project with the given files.

If you have any questions or problems, don't hesitate to contact me.

cjweeks
  • 267
  • 1
  • 4
  • 8
  • Why do we need to put the files in the /usr/local? Can I just put my files somewhere else, and have Cmake find them directly instead? Thanks for the help with cmake – Pototo Apr 03 '17 at 23:08
  • The linked project doesn't work with TF 1.4+ (the current TF version is 2.8). – rustyx Apr 11 '22 at 11:30
5

If you're on MacOS, using homebrew, CMake and pkg_config it's easy.

First get Tensorflow using brew:

brew install libtensorflow

Then in CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(tf-inference)

find_package(PkgConfig)
pkg_check_modules(TensorFlow REQUIRED tensorflow)

link_directories(${TensorFlow_LIBRARY_DIRS})
include_directories(${TensorFlow_INCLUDE_DIRS})
add_compile_definitions(${TensorFlow_CFLAGS_OTHER})

add_executable(tf-inference inference.cpp)
target_link_libraries(tf-inference ${TensorFlow_LIBRARIES})
Roy Shilkrot
  • 3,079
  • 29
  • 25