[英]How to use TensorFlow C++ api as a shared library with bazel?
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
. 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 ?我对bazel不熟悉,我应该如何使用那些带有bazel的TF共享库来编写C++代码,你能提供一个这样的例子吗?
Bazel is just a tool like make or cmake. Bazel只是一个类似于 make 或 cmake 的工具。 For compiling your code using bazel, you have to create a build file BUILD
.要使用 bazel 编译代码,您必须创建一个构建文件BUILD
。 Suppose that your code is at, eg, tensorflow/tensorflow/test/code.cpp
:假设您的代码位于例如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:创建一个包含如下内容的BUILD
文件(在同一文件夹中):
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.可执行文件将在 tensorflow 根目录中的bazel-bin
文件夹中可用。
However, the compiled code can be huge (> 100 MB) so it is better to link your code to TensorFlow's shared libraries.但是,编译后的代码可能很大(> 100 MB),因此最好将您的代码链接到 TensorFlow 的共享库。 Both libtensorflow_cc.so
and libtensorflow_framework.so
can be used in your codes like other shared library files ( .so
). libtensorflow_cc.so
和libtensorflow_framework.so
都可以像其他共享库文件 ( .so
) 一样在您的代码中使用。 The following is a common way to do so.以下是执行此操作的常用方法。
.h
) using the shared library ( libtensorflow_cc.so
or/and libtensorflow_framework.so
), eg,使用共享库( libtensorflow_cc.so
或/和libtensorflow_framework.so
),使用 header 库文件 ( .h
) 编译 C++ 代码,例如, g++ -fPIG -Wall -o code code.cpp -L/usr/lib/libtensorflow_cc.so -ltensorflow_cc
LD_LIBRARY_PATH
to the location of .so
files将LD_LIBRARY_PATH
设置为.so
文件的位置ldd your_code.out
to confirm if your executable is properly linked to a shared library.使用ldd your_code.out
来确认您的可执行文件是否正确链接到共享库。./your_code.out
)运行可执行文件(例如./your_code.out
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.