Issue
Now I can build TensorFlow C++ api as a shared library refer to make shared libraries with Bazel at Tensorflow, libtensorflow_cc.so
and libtensorflow_framework.so
file can be locate at bazel-bin/tensorflow
.
I am not familiar with bazel, how should I use those TF shared library with bazel to write C++ code, could you please provide an example like that links?
Solution
Bazel is just a tool like make or cmake. For compiling your code using bazel, you have to create a build file BUILD
. Suppose that your code is at, e.g., tensorflow/tensorflow/test/code.cpp
:
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"
using namespace tensorflow;
...
create a BUILD
file (in the same folder) containing contents like this:
cc_binary(
name = "code",
srcs = ["code.cc"],
deps = [
"//tensorflow/core:tensorflow",
]
)
then configure and compile your code using command:
cd tensorflow
./configure
cd tensorflow/tensorflow/test
bazel build :code
An executable will be available at bazel-bin
folder in tensorflow root directory.
However, the compiled code can be huge (> 100 MB) so it is better to link your code to TensorFlow's shared libraries. Both libtensorflow_cc.so
and libtensorflow_framework.so
can be used in your codes like other shared library files (.so
). The following is a common way to do so.
- Compile the C++ code using the header library file (
.h
) using the shared library (libtensorflow_cc.so
or/andlibtensorflow_framework.so
), e.g.,g++ -fPIG -Wall -o code code.cpp -L/usr/lib/libtensorflow_cc.so -ltensorflow_cc
- Set
LD_LIBRARY_PATH
to the location of.so
files - Use
ldd your_code.out
to confirm if your executable is properly linked to a shared library. - Run the executable (e.g.
./your_code.out
)
Answered By - High Performance Rangsiman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.