linux 下 tensorflow C++ 提取include檔案、第一個hello world
在用C++ 呼叫tensorflow庫時,發現tensorflow 官網沒有介紹如何獲取tensorflow庫標頭檔案,導致在呼叫tensorflow 庫的時候遇見了很多問題。
如果還沒有編譯tensorflow庫,可以看下:https://www.cnblogs.com/GengMingYan/p/15963832.html
tensorflow : tensorflow 1.15.4
gcc : gcc version 11.2.0
tensorflow "Hello World"
1. 首先,先附上最簡單的程式碼,類似tensorflow 版 "Hello World"
#include <stdlib.h> #include"tensorflow/core/public/session.h" using namespace tensorflow; using namespace std; static Status ReadEntireFile(tensorflow::Env* env, const string& filename, Tensor* output); int main(int argc, char *argv[]) { SessionOptions sessionOptions; Session *session = NewSession(sessionOptions);return EXIT_SUCCESS; }
最終編譯命令:
g++ test.cpp -Iport_def -Ieigen_archive -L/opt/work/c_work/qt_work/tensorflow_cc_demo/lib2 -I. -ltensorflow_cc -ltensorflow_framework
/opt/work/c_work/qt_work/tensorflow_cc_demo/lib2 是我本機 tensorflow 庫所在位置
整體過程
首先,需要先編譯tensorflow,因為有很多需要用到的標頭檔案是在編譯後生成的,標頭檔案生成在~/cache/bazel/資料夾下
專案使用tensorflow,就需要兩個庫:
1. 從原始碼中複製tensorflow/core到專案目錄
2. 從原始碼中複製third_party到專案目錄
3. 從~/.cache/bazel/_bazel_root/中複製用到的各個標頭檔案到專案目錄 (比較繁瑣的地方,不難,但需要費很大精力解決各種 檔案不存在錯誤)
開始
1. 建立測試檔案
建立一個資料夾,建立一個test.cpp檔案。把上方tensorflow 的"hello world"內容複製到test.cpp中,像這樣:
編譯命令
g++ test.cpp -Iport_def -Ieigen_archive -L/opt/work/c_work/qt_work/tensorflow_cc_demo/lib2 -I. -ltensorflow_cc -ltensorflow_framework
2. 複製原始碼中的tensorflow/core和third_party資料夾
複製原始碼中tensorflow/core資料夾和third_party資料夾到專案目錄下
這時候如果執行命令編譯,會報device_attributes.pb.h檔案不存在:
In file included from test.cpp:2: tensorflow/core/public/session.h:22:10: fatal error: tensorflow/core/framework/device_attributes.pb.h: 沒有那個檔案或目錄 22 | #include "tensorflow/core/framework/device_attributes.pb.h" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated.
3. device_attributes.pb.h
搜尋device_attributes.pb.h檔案,找到device_attributes.pb.h檔案層級目錄為tensorflow/core/framework/的結果:
把 device_attributes.pb.h檔案所在目錄framework資料夾和專案中tensorflow/core/下的framework資料夾合併
在編譯,會報:
In file included from tensorflow/core/public/session.h:22, from test.cpp:2: ./tensorflow/core/framework/device_attributes.pb.h:10:10: fatal error: google/protobuf/port_def.inc: 沒有那個檔案或目錄 10 | #include <google/protobuf/port_def.inc> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated.
4. port_def.inc
搜尋port_def.inc檔案,找到檔案層級是google/prootobuf的目錄:
專案路徑下建立port_def目錄,複製google到port_def路徑下
在編譯,報錯:
In file included from ./tensorflow/core/framework/tensor.h:22, from tensorflow/core/public/session.h:24, from test.cpp:2: ./third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:10: fatal error: unsupported/Eigen/CXX11/Tensor: 沒有那個檔案或目錄 1 | #include "unsupported/Eigen/CXX11/Tensor" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated.
5. Tensor
全域性搜尋Tensor,找到層級目錄eigen_archive/unsupported/Eigen/CXX11/下的Tensor檔案。
複製eigen_archive到專案目錄下
在編譯,報錯:
In file included from ./tensorflow/core/framework/tensor.h:23, from tensorflow/core/public/session.h:24, from test.cpp:2: ./tensorflow/core/framework/allocator.h:24:10: fatal error: absl/strings/string_view.h: 沒有那個檔案或目錄 24 | #include "absl/strings/string_view.h" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated.
6. string_view.h
全域性搜尋string_view.h,找到層級目錄absl/strings下的string_view.h檔案:
複製absl目錄到專案目錄下。
在編譯,報錯:
In file included from ./tensorflow/core/platform/errors.h:22, from ./tensorflow/core/lib/core/errors.h:19, from ./tensorflow/core/framework/tensor_shape.h:23, from ./tensorflow/core/framework/tensor.h:24, from tensorflow/core/public/session.h:24, from test.cpp:2: ./tensorflow/core/platform/status.h:28:10: fatal error: tensorflow/core/protobuf/error_codes.pb.h: 沒有那個檔案或目錄 28 | #include "tensorflow/core/protobuf/error_codes.pb.h" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated.
7. error_codes.pb.h
全域性搜尋error_codes.pb.h,找到層級目錄tensorflow/core/protobuf下的error_codes.pb.h:
複製protobuf目錄下檔案到專案目錄tensorflow/core/protobuf下。
最後,編譯成功。
已把整理好的1.15.4標頭檔案放到: https://quqi.avyeld.com/s/6273754/gRyB9OxneCnE6PJY
如果在引入標頭檔案過程中,用到一些第三方庫沒有,儘量從編譯環境下找,不要去網上下載,大概率會有版本不對問題。