tensorflow c++编译

1.     安装bazel ,可以在bazel官网上查找到具体安装方法
2.     git clone 一份tensorflow的源码 上官网看,(需要clone相对稳定的版本如r1.3,否则可能会存在编译错误,)
3.     进入tensorflow的根目录  
3.1    执行./configure 根据提示配置一下环境变量这个官网上有类似的,对于环境变量的配置可以参考官网,

       要GPU的话要下载nvidia驱动的 尽量装最新版的驱动吧 还有cudnn version为5以上的 这些在官网都有提及的

$ cd tensorflow  # cd to the top-level directory created
$ ./configure
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/lib/python2.7/dist-packages]

Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with MKL support? [y/N]
No MKL support will be enabled for TensorFlow
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA support will be enabled for TensorFlow
Do you wish to build TensorFlow with VERBS support? [y/N]
No VERBS support will be enabled for TensorFlow
Do you wish to build TensorFlow with OpenCL support? [y/N]
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] Y
CUDA support will be enabled for TensorFlow
Do you want to use clang as CUDA compiler? [y/N]
nvcc will be used as CUDA compiler
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 6
Please specify the location where cuDNN 6 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: 3.0
Do you wish to build TensorFlow with MPI support? [y/N] 
MPI support will not be enabled for TensorFlow
Configuration finished

3.2    有显卡的执行 " bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so "
       没显卡的 " --config=cuda " 就不要加了
       如果是c版本的tensorflow, 把" libtensorflow_cc " 改成 " libtensorflow " 

       这句命令其实是bazel的用法  具体要生成哪个可以 " vim $(TF_ROOT_PATH)/tensorflow/BUILD " 查看

有写问题一种是tensorflow太新可能编译不通过,一种可能是bazel太新导致有些错误。如:

_WARNING: ignoring http_proxy in environment.
ERROR: /root/.cache/bazel/bazel_root/6093305914d4a581ed00c0f6c06f975b/external/io_bazel_rules_closure/closure/private/defs.bzl:27:16: Theset constructor for depsets is deprecated and will be removed. Please use thedepset constructor instead. You can temporarily enable the deprecatedset constructor by passing the flag --incompatible_disallow_set_constructor=false.
ERROR: error loading package '': Extension file 'closure/private/defs.bzl' has errors.

4.     一般是不报错的   如果报错要么是依赖项没有 到时候一个个装就好了  也有个错误是说 protoc 版本太低 这时候更新一下protoc就好了
5.     bazel build成功后会有提示的 然后拷贝一下头文件  (这里应该没落下, 如果有找不到的话还得再找找吧- -)
       cp bazel-bin/tensorflow/libtensorflow_cc.so $(YOUR_PATH)
       cp -r bazel-genfiles/* $(YOUR_PATH)

       cp -r tensorflow/ $(YOUR_PATH)

5、库的使用

在使用tensorflow c/c++接口时,会有很多头文件依赖、protobuf版本依赖等问题

(1)tensorflow/contrib/makefile目录下,找到build_all_xxx.sh文件并执行,例如准备在linux上使用,就执行build_all_linux.sh文件,成功后会出现一个gen文件夹

(2)把tensorflow和bazel-genfiles文件夹下的头文件都抽取出来放在一个文件夹下面,或者通过cmake把这两个路径添加进include_directories

(3)tensorflow/contrib/makefile/gen/protobuf/include,也就是(1)中生成的文件夹中的头文件,也需要抽取或者在cmake中包含在include_directories

代码:

#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";
}

cmake:
 cmake_minimum_required (VERSION 2.8.8)
 project (tf_example)
 
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -W")
 aux_source_directory(./src DIR_SRCS)
 link_directories(./lib)
 include_directories(
   path_to_tensorflow/tensorflow
   path_to_tensorflow/tensorflow/bazel-genfiles
   path_to_tensorflow/tensorflow/contrib/makefile/gen/protobuf/include
   ) add_executable(tf_test  ${DIR_SRCS}) target_link_libraries(tf_example tensorflow_cc)

接下来执行命令:

cd build

cmake ..

make

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值