Tensorflow c api 编译安装

Ubuntu 16.04

Tensorflow 1.10.0

 

1.编译动态链接库文件:

$ ./configure
$ bazel build -c dbg //tensorflow:libtensorflow_cc.so

原先编译python 版本的whl时,是不会有libtensorflow_cc.so的,所以还是要重新编译。磁盘要足够大啊,要不然不可能成功。
由于是debug版本link的时候,装了7个小时,release版本就不会有这个问题。

2.配置外部依赖:

$ cd tensorflow/contrib/makefile 
$ ./build_all_linux.sh

# 若出现 /autogen.sh: 4: autoreconf: not found 错误,安装 sudo apt-get install autoconf automake libtool

3.配置到/usr/local/include/tf 中

$ find tensorflow/core -name '*.h' | xargs -i cp --parents '{}' /usr/local/include/tf 
$ cp -r bazel-genfiles/* /usr/local/include/tf 
$ cp -r tensorflow/contrib/makefile/gen/protobuf/include/* /usr/local/include/tf 
$ cp -r tensorflow/contrib/makefile/downloads/eigen/* /usr/local/include/tf 
$ cp -r tensorflow/contrib/makefile/downloads/nsync/public/* /usr/local/include/tf 
$ cp -r third_party /usr/local/include/tf 
$ cp tensorflow/bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib 
$ cp tensorflow/bazel-bin/tensorflow/libtensorflow_framework.so /usr/local/lib

参考:https://spockwangs.github.io/blog/2018/01/13/train-using-tensorflow-c-plus-plus-api/ 

这个里面还有静态链接的方式,亲测好用。

4.运行一个例子:

  1 #include "tensorflow/core/platform/env.h"
  2 #include "tensorflow/core/public/session.h"
  3 #include <iostream>
  4 
  5 using namespace std;
  6 using namespace tensorflow;
  7 
  8 int main()
  9 {
 10     Session *session;
 11     Status status = NewSession(SessionOptions(), &session);
 12     if (!status.ok()){
 13         cout << status.ToString()<<"\n";
 14         return 1;
 15     }
 16     cout << "Session successfully created. \n";
 17     return 0;
 18 
 19 }
g++ -std=c++11 tf_test.cpp -o tf_test -I/usr/local/include/tf -ltensorflow_cc -ltensorflow_framework

  报错了竟然:

/usr/local/include/tf/tensorflow/core/lib/core/error_codes.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is
#error This file was generated by a newer version of protoc which is

 发现是安装的protobuf 版本不一致。需要的是3.6.0.所以直接把,download_dependencies.sh 中的protobuf_url改掉,改成3.6 版本就好了

重新运行,build_all_liunx 

 注意在这之前需要ldconfig ,要不然链接不上.so 文件

 这次就好啦!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值