TensorFlow基礎筆記(15) 編譯TensorFlow.so,提供給C++平台調用


參考 

http://blog.csdn.net/rockingdingo/article/details/75452711

https://www.cnblogs.com/hrlnw/p/7007648.html

https://www.cnblogs.com/hrlnw/p/7383951.html

 

1.下載tensorflow源碼

  git clone  https://github.com/tensorflow/tensorflow

2.安裝bazel

sudo apt-get update && sudo apt-get install bazel  

升級bazel

sudo apt-get upgrade bazel

使用bazel編譯出現最低版本要求 0.4.5, 而本身的版本為0.11.1的時候,

將 最低版本要求 0.4.5文件中的0.4.5修改為0.0.0

3.編譯源碼,生成so庫

## 進入tensoflow源碼根目錄后編譯
# 編譯生成.so文件, 編譯C++ API的庫 (建議)
bazel build //tensorflow:libtensorflow_cc.so

# 也可以選擇,編譯C API的庫
bazel build //tensorflow:libtensorflow.so

編譯出現問題,建議Git clone最新的tensoflow代碼 重新編譯

編譯完成,可以看到編譯生成的文件路徑

tensorflow/cc/gradients/data_flow_grad.cc:151:1: note: in expansion of macro 'REGISTER_GRADIENT_OP'
REGISTER_GRADIENT_OP("ParallelDynamicStitch", DynamicStitchGrad);
^
Target //tensorflow:libtensorflow_cc.so up-to-date:
bazel-bin/tensorflow/libtensorflow_cc.so
INFO: Elapsed time: 1051.314s, Critical Path: 74.06s
INFO: Build completed successfully, 2355 total actions

 

4 編譯完成,配准調用環境

在等待30多分鍾后, 如果編譯成功,在tensorflow根目錄下出現 bazel-bin, bazel-genfiles 等文件夾,

按順序執行以下命令將對應的libtensorflow_cc.so文件和其他文件拷貝進入 /usr/local/lib/ 目錄

mkdir /usr/local/include/tf  

cp -r bazel-genfiles/ /usr/local/include/tf/  

cp -r tensorflow /usr/local/include/tf/  

cp -r third_party /usr/local/include/tf/  

cp -r bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib/ 

這一步完成后,我們就准備好了libtensorflow_cc.so文件等,后面在自己的C++編譯環境和代碼目錄下編譯時鏈接這些庫即可。

 

5 c++調用libtensorflow_cc.so

 

 

 

安裝protobuf出現了問題

+ autoreconf -f -i -Wall,no-obsolete
configure.ac:30: error: possibly undefined macro: AC_PROG_LIBTOOL
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
autoreconf: /usr/bin/autoconf failed with exit status: 1

網上的方案

https://superuser.com/questions/565988/autoconf-libtool-and-an-undefined-ac-prog-libtool

已經安裝了 libtool 

sudo apt-get install libtool (失敗)

可以解決的方法

You need to reinstall it in order to fix the error so follow these steps :

1] Remove current libtool if installed: sudo apt-get purge libtool

2] Download it from official website https://www.gnu.org/software/libtool/

3] Untar it: tar -xzvf "name of the tar_file"

4] Enter folder and type: ./configure && make

5] Install it: sudo make install

And you are done, error should be fixed !

 

安裝步驟

git clone https://github.com/google/protobuf.git 

(0)./autogen.sh   

(1)./configure  --prefix=/usr/local/protobuf  

(2)make   

(3)make check   

(4)make install  

 

注意:
安裝成功后,將它的bin和lib目錄分別加入到PATH和LD_LIBRARY_PATH環境變量,以方便直接調用。
設置環境變量過程:編輯/etc/profile,在文件末尾添加:

注意:這里添加的路徑不同版本protobuf可能不同,需要根據提示確認路徑,否則會出現root用戶和普通用戶版本不同的問題。

export PATH=$PATH:/usr/local/protobuf/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib
此時最好重啟一次。

查看protobuf版本的命令為:protoc --version,如果普通用戶無法查看,或者與sudo protoc --version查看的版本不同,就是出現了上述路徑添加錯誤的情況。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM