Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use pre-compiled tensorflow with cmake

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.

like image 429
Amelse Etomer Avatar asked Jun 28 '16 12:06

Amelse Etomer


2 Answers

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.

like image 189
cjweeks Avatar answered Oct 19 '22 09:10

cjweeks


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})
like image 21
Roy Shilkrot Avatar answered Oct 19 '22 10:10

Roy Shilkrot