参考博客http://www.cnblogs.com/hrlnw/p/7383951.html
1. Install JDK 8
(ubuntu16.04)
sudo apt-get install openjdk-8-jdk
(ubuntu14.04 )
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install oracle-java8-installer
2. Add Bazel distribution URI as a package source (one time setup)
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
(If you want to install the testing version of Bazel, replace stable with testing.)
3. Install and update Bazel
sudo apt-get update && sudo apt-get install bazel
(Once installed, you can upgrade to a newer version of Bazel with:)
sudo apt-get upgrade bazel
2.进入tensorflow_r1.4根目录 执行 ./configure 根据需求选择Y/N(参考tensorflow installing from sources官网)
3.编译C++版本动态链接库/C版本
进入路径 /tensorflow-master/tensorflow 下 执行:
C版本:
bazel build :libtensorflow.so
C++版本:
bazel build :libtensorflow_cc.so
i7大约需要 50 分钟
编译成功后,在bazel-bin/tensorflow/目录下会出现libtensorflow.so/libtensorflow_cc.so文件
4.其他依赖
在使用tensorflow c/c++接口时,会有很多头文件依赖、protobuf版本依赖等问题
tensorflow/contrib/makefile目录下,找到build_all_xxx.sh文件并执行,例如准备在linux上使用,就执行build_all_linux.sh文件,成功后会出现一个gen文件夹 大约需要30分钟
5.编写代码和CMakeLists.txt
tf_test.cpp
CMakelists.txt
一般来说编译时找不到某个文件 可以在tensorflow的根目录下面搜索它,然后添加到include里面,bazel-×××几个文件夹里面也会有相应的头文件(因为权限问题可能得单独搜一下,在父目录里面好像搜不到子目录里root权限的文件),都找一下。。
要求:ubuntu14.04 tensorflow r1.4(按照官网教程安装并运行成功)
可以在ubuntu中使用cmake make编译C++工程,调用TensorFlow库函数及其训练好的神经网络模型
1. Install JDK 8
(ubuntu16.04)
sudo apt-get install openjdk-8-jdk
(ubuntu14.04 )
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install oracle-java8-installer
2. Add Bazel distribution URI as a package source (one time setup)
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
(If you want to install the testing version of Bazel, replace stable with testing.)
3. Install and update Bazel
sudo apt-get update && sudo apt-get install bazel
(Once installed, you can upgrade to a newer version of Bazel with:)
sudo apt-get upgrade bazel
2.进入tensorflow_r1.4根目录 执行 ./configure 根据需求选择Y/N(参考tensorflow installing from sources官网)
3.编译C++版本动态链接库/C版本
进入路径 /tensorflow-master/tensorflow 下 执行:
C版本:
bazel build :libtensorflow.so
C++版本:
bazel build :libtensorflow_cc.so
i7大约需要 50 分钟
编译成功后,在bazel-bin/tensorflow/目录下会出现libtensorflow.so/libtensorflow_cc.so文件
4.其他依赖
在使用tensorflow c/c++接口时,会有很多头文件依赖、protobuf版本依赖等问题
tensorflow/contrib/makefile目录下,找到build_all_xxx.sh文件并执行,例如准备在linux上使用,就执行build_all_linux.sh文件,成功后会出现一个gen文件夹 大约需要30分钟
5.编写代码和CMakeLists.txt
tf_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";
}
CMakelists.txt
cmake_minimum_required (VERSION 2.8)
project (tf_example)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -W")
include_directories(
/home/xxx/tensorflow-master
/home/xxx/tensorflow-master/tensorflow/bazel-genfiles
/home/xxx/tensorflow-master/tensorflow/contrib/makefile/gen/protobuf/include
/home/xxx/tensorflow-master/tensorflow/contrib/makefile/gen/host_obj
/home/xxx/tensorflow-master/tensorflow/contrib/makefile/gen/proto
/home/xxx/tensorflow-master/tensorflow/contrib/makefile/downloads/nsync/public
/home/xxx/tensorflow-master/tensorflow/contrib/makefile/downloads/eigen
/home/xxx/tensorflow-master/bazel-out/local_linux-py3-opt/genfiles
)
add_executable(tf_test tf_test.cpp)
target_link_libraries(tf_test
/home/xxx/tensorflow-master/bazel-bin/tensorflow/libtensorflow_cc.so
/home/xxx/tensorflow-master/bazel-bin/tensorflow/libtensorflow_framework.so
)
一般来说编译时找不到某个文件 可以在tensorflow的根目录下面搜索它,然后添加到include里面,bazel-×××几个文件夹里面也会有相应的头文件(因为权限问题可能得单独搜一下,在父目录里面好像搜不到子目录里root权限的文件),都找一下。。