ubuntu使用bazel编译安装tensorflow c++

按照官网步骤
1.下载安装cuda,cudnn(注意版本,我的是cuda9.0+cudnn7.5)
2.下载安装bazel,也需要注意版本
3.下载tensorflow源码https://github.com/tensorflow/tensorflow
4.进入tensorflow文件夹,

cd tensorflow
./configure

config按照提示选择,若有gpu,怎在选择cuda选项时选Y
5. bazel编译

bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so  #有显卡
bazel build --config=opt  //tensorflow:libtensorflow_cc.so #无显卡

编译需要等待较长时间
编译结束后,两个so库在bazel-bin/tensorflow文件夹下libtensorflow_cc.so 和 libtensorflow_framework.so
6. 安装其它依赖
在tensorflow/contrib/makefile下,执行build_all_linux.sh文件,成功后会出现一个gen文件夹
如果tensorflow/contrib/makefile下没有downloads文件夹,则回到tensorflow文件夹下,执行

./tensorflow/contrib/makefile/download_dependencies.sh

build_all_linux.sh 包含调用 download_dependencies.sh,download_dependencies.sh为下载一些依赖,如eigen和protobuf, absl,nsync等,在tensorflow目录下workspace.bzl文件有相关下载源和版本。
7.新建CMakeLists.txt

cmake_minimum_required (VERSION 2.8.8)
project (tf_example)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -W")
link_directories(./lib)
include_directories(
  /path/to/tf
  tf/bazel-genfiles
  tf/bazel-bin/tensorflow
  tf/tensorflow/contrib/makefile/downloads/nsync/public
  tf/tensorflow/contrib/makefile/gen/protobuf/include
   /usr/local/include/eigen3
  tf/tensorflow/contrib/makefile/downloads/absl
)
add_executable(tf_test  tf.cpp)
target_link_libraries(tf_test tensorflow_cc tensorflow_framework)

编译运行:

mkdir build
cd build
cmake ..
make 
./tf_test 

参考链接:
https://blog.csdn.net/qq_29462849/article/details/84986592
https://www.e-learn.cn/content/linux/779942

你可能感兴趣的:(tf)