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 文件
这次就好啦!