Tensorflow C++动态库编译
Tensorflow编译成C++动态库
本机配置环境
Ubuntu 16.04
Python 2.7
CUDA 9.0
cuDNN 7
Bazel 0.17.2
TensorFlow 1.10
protobuf 3.5.0
GPU: RTX2080
安装bazel
-
安装依赖
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python
-
下载bazel
下载bazel--installer-linux-x86_64.sh,官方github链接link
-
编译bazel
chmod +x bazel--installer-linux-x86_64.sh
./bazel--installer-linux-x86_64.sh --user -
配置环境
将下面的命令行添加到~/.bashrc中export PATH=“HOME/bin”
编译Protobuf
- 下载并解压缩 protobuf-cpp-3.5.0.tar.gz文件 link
-
进入解压后的文件夹
cd protobuf-3.5.0
-
安装, 建议将protobuf安装在/usr/protobuf下。请先在/usr下新建一个名为protobuf的文件夹,此即为最终的安装路径。
sudo mkdir /usr/protobuf
./autogen.sh ##下载自github的代码需要执行此行来生成configure文件
./configure --prefix=/usr/protobuf
make
make check
sudo make install -
配置:
sudo vim /etc/profile, 在/etc/profile中添加下面内容#(动态库搜索路径) 程序加载运行期间查找动态链接库时指定除了系统默认路径之外的其他路径
export LD_LIBRARY_PATH= $LD_LIBRARY_PATH:/usr/protobuf/lib/
#(静态库搜索路径) 程序编译期间查找动态链接库时指定查找共享库的路径
export LIBRARY_PATH=$LIBRARY_PATH:/usr/protobuf/lib/
#执行程序搜索路径
export PATH=$PATH:/usr/protobuf/bin/
#c程序头文件搜索路径
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/protobuf/include/
#c++程序头文件搜索路径
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/protobuf/include/
#pkg-config 路径
export PKG_CONFIG_PATH=/usr/protobuf/lib/pkgconfig/
-
编译Tensorflow
-
下载tensorflow源码
git clone [email protected]:tensorflow/tensorflow.git
-
配置环境
- 选择版本
git checkout r1.10
- 进入tensorflow文件夹
cd tensorflow
- tensorflow配置
./configure
- 具体的配置如下
- 选择版本
-
进入 tensorflow 目录进行编译,编译成功后,在 /bazel-bin/tensorflow 目录下会出现 libtensorflow_cc.so 文件
C版本: bazel build :libtensorflow.so
C++版本: bazel build :libtensorflow_cc.so -
编译其他依赖
- 进入 tensorflow/contrib/makefile 目录下,运行 ./build_all_linux.sh,成功后会出现一个gen文件夹
- 若出现如下错误 /autogen.sh: 4: autoreconf: not found ,安装相应依赖即可 sudo apt-get install autoconf automake libtool
测试编译结果
-
Cmaklist.txt
cmake_minimum_required(VERSION 2.8)
project(Tensorflow_test)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES test.cpp)
include_directories( /home/hp/tensorflow
/home/hp/tensorflow/bazel-genfiles
/home/hp/tensorflow/tensorflow/contrib/makefile/gen/protobuf/include
/home/hp/tensorflow/tensorflow/contrib/makefile/gen/host_obj
/home/hp/tensorflow/tensorflow/contrib/makefile/gen/proto
/home/hp/tensorflow/tensorflow/contrib/makefile/downloads/nsync/public
/home/hp/tensorflow/tensorflow/contrib/makefile/downloads/eigen
/home/hp/tensorflow/bazel-out/local_linux-py3-opt/genfiles
/home/hp/tensorflow/tensorflow/contrib/makefile/downloads/absl )
add_executable(Tensorflow_test ${SOURCE_FILES})
target_link_libraries(
Tensorflow_test /home/hp/tensorflow/bazel-bin/tensorflow/libtensorflow_cc.so
/home/hp/tensorflow/bazel-bin/tensorflow/libtensorflow_framework.so ) -
test.cpp
#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>
#include < iostream>
using namespace std;
using namespace tensorflow;
int main() {
Session* session;
Status status = NewSession(SessionOptions(), &session);
if (!status.ok()) {
cout << status.ToString() << “\n”;
return 1;
}
cout << “Session successfully created.\n”;
return 0;
} -
测试tensorflow C++编译
- 编译命令
cmake .
make
./test - 测试结果
- 编译命令