6

I've been developing c++ project using a Tensorflow c++ api. it just execute created tensorflow's graph from Python. I build it using bazel with Tensorflow code now. But I think it's inefficient way.

I want just Tensorflow library and header files, and Just compile my project only using Cmake.

I know how to build shared library.

bazel build -c opt --config=cuda //tensorflow:libtensorflow.so but this command just make a libtensorflow.so file. I can't find header files for build my project.

Is there way to package tensorflow library for c++? such as mvn package command.

Ben
  • 95
  • 1
  • 5
  • 1
    Possible duplicate of [How to build and use Google TensorFlow C++ api](http://stackoverflow.com/questions/33620794/how-to-build-and-use-google-tensorflow-c-api) – m8mble Nov 10 '16 at 08:33
  • @m8mble The questions are close, indeed. It seems to me more specific to CMake here. Should we ask for an extra tag or something in the title? – Eric Platon Nov 10 '16 at 08:37
  • 1
    @EricPlaton Yes, this would atleast help. In any case, the question lacks distinction from the old one... – m8mble Nov 10 '16 at 08:40

5 Answers5

3

As far as I know, there is no official distributable C++ API package. There is, however, tensorflow_cc project that builds and installs TF C++ API for you, along with convenient CMake targets you can link against. According to your description, that may be just what you need.

Floop
  • 451
  • 4
  • 10
2

If your operating system is Debian or Ubuntu, you can download unofficial prebuilt packages with the Tensorflow C/C++ libraries. This distribution can be used for C/C++ inference with CPU, GPU support is not included:

https://github.com/kecsap/tensorflow_cpp_packaging/releases

There are instructions written how to freeze a checkpoint in Tensorflow (TFLearn) and load this model for inference with the C/C++ API:

https://github.com/kecsap/tensorflow_cpp_packaging/blob/master/README.md

Beware: I am the developer of this Github project.

As Floop already mentioned, his tensorflow_cc project is also a good alternative without packaging, especially if you want GPU support for the inference.

kecsap
  • 350
  • 3
  • 8
1

You can build tensorflow with CMake. This also creates a TensorflowConfig.cmake, which you can integrate in your project

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/cmake

Little hint: You have to build the shared lib, even if you do not need it.

0

You have two option: static linking and dynamic linking. If you want to dynamic link your c++ project to TensorFlow, all you need is a --whole-archive linker flag. The necessary header files are provided by a pip install.

Generating the library is basically

bazel build -c opt --copt=-mfpmath=both --config=cuda //tensorflow:libtensorflow.so
bazel build -c opt --copt=-mfpmath=both --config=cuda //tensorflow:libtensorflow_cc.so

Having everything in place it is easy to run a TensorFlow graph in C, C++, Go (GitHub project). See the linked project for these working examples in C, C++, Go.

Patwie
  • 4,360
  • 1
  • 21
  • 41
-1

When building against the shared library, the headers I use are in $PROJECT_HOME/bazel-genfiles.

Adding $PROJECT_HOME/bazel-genfiles to the linker header list should be enough.

Eric Platon
  • 9,819
  • 6
  • 41
  • 48