参考这篇文章安装,依次安装bazel,protocbuf,eigen3,然后下载tensorflow源码,编译c++ api,将编译结果拷贝到搜索路径
最后测试案例时遇到一些问题
(1)fatal error: absl/strings/string_view.h
解决方案,git clone https://github.com/abseil/abseil-cpp,然后把该库加到搜索目录里面
(2)对‘tensorflow::SessionOptions::SessionOptions()’未定义的引用
找不到正确的libtensorflow_cc.so,添加动态链接库路径
(3)tensorflow::status::tostring[abi:cxx11]() const
解决方案,编译选项设置-D_GLIBCXX_USE_CXX11_ABI=0
最终测试代码
1 #include <tensorflow/core/platform/env.h> 2 #include <tensorflow/core/public/session.h> 3 4 #include <iostream> 5 6 using namespace std; 7 using namespace tensorflow; 8 9 int main() 10 { 11 Session* session; 12 Status status = NewSession(SessionOptions(), &session); 13 if (!status.ok()) { 14 cout << status.ToString() << " "; 15 return 1; 16 } 17 cout << "Session successfully created. "; 18 }
编译代码
g++ -g -D_GLIBCXX_USE_CXX11_ABI=0 test_tf.cpp -o tfcpp_demo -I /usr/include/eigen3 -I /usr/local/include/tf -L/usr/local/lib/ `pkg-config --cflags --libs protobuf` -ltensorflow_cc -ltensorflow_framework