When you want to build your C++ program or application using TensorFlow library or functions, you probably will encounter some header file missed issues or linking problems. Here is the step list that I have verified and it works well.
1. Prepare TensorFlow ( v1.10) and its third party's library
2. Modify .tf_.tf_configure.bazelrc
3. Build TensorFlow C++ APIs library
4. Setup header file and library
Then, I provide a simple project and CMakeLists.txt for your reference as follows:
https://github.com/teyenliu/dnn_tensorflow_cpp
If you git clone it, you will get this in the folder:
├── BUILD
├── CMakeLists.txt
├── data_set.cc
├── data_set.h
├── model.cc
├── normalized_car_features.csv
└── README.md
My CMakeLists.txt is here:
Finally, I can build and run it successfully.
P.S: For the more in details about the C++ example, please check out this blog:
https://matrices.io/training-a-deep-neural-network-using-only-tensorflow-c/
P.S: If you encounter some errors, please check out the following link to seek for the solution:
http://www.liuxiao.org/2018/08/ubuntu-tensorflow-c-%E4%BB%8E%E8%AE%AD%E7%BB%83%E5%88%B0%E9%A2%84%E6%B5%8B1%EF%BC%9A%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/
1. Prepare TensorFlow ( v1.10) and its third party's library
$ git clone --recursive https://github.com/tensorflow/tensorflow
$ cd tensorflow/contrib/makefile
$ ./build_all_linux.sh
2. Modify .tf_.tf_configure.bazelrc
$ cd tensorflow/
$ vim .tf_configure.bazelrc
append this line in the bottom of the file
==>
build --define=grpc_no_ares=true
3. Build TensorFlow C++ APIs library
$ cd tensorflow
$ ./configure
<<< Please based on your requirement to configure the items in this step >>>
$ bazel build --config opt //tensorflow:libtensorflow_cc.so --cxxopt="-std=c++11" --copt="-O3"
4. Setup header file and library
sudo mkdir /usr/local/tf_libs
sudo mkdir /usr/local/tf_libs/include
sudo cp -r tensorflow/contrib/makefile/downloads/eigen/Eigen /usr/local/tf_libs/include/
sudo cp -r tensorflow/contrib/makefile/downloads/eigen/unsupported /usr/local/tf_libs/include/
sudo cp -r tensorflow/contrib/makefile/downloads/absl/absl /usr/local/tf_libs/include/
sudo cp -r tensorflow/contrib/makefile/gen/protobuf/include/google /usr/local/tf_libs/include/
sudo cp tensorflow/contrib/makefile/downloads/nsync/public/* /usr/local/tf_libs/include/
sudo cp -r bazel-genfiles/tensorflow /usr/local/tf_libs/include/
sudo cp -r tensorflow/cc /usr/local/tf_libs/include/tensorflow
sudo cp -r tensorflow/core /usr/local/tf_libs/include/tensorflow
sudo mkdir /usr/local/tf_libs/include/third_party
sudo cp -r third_party/eigen3 /usr/local/tf_libs/include/third_party/
sudo mkdir /usr/local/tf_libs/lib
sudo cp bazel-bin/tensorflow/libtensorflow_*.so /usr/local/tf_libs/lib
#sudo cp bazel-bin/tensorflow/contrib/lite/lib*.so /usr/local/tf_libs/lib
If you finish the steps above, you are able to build your C++ program or application from now on.Then, I provide a simple project and CMakeLists.txt for your reference as follows:
https://github.com/teyenliu/dnn_tensorflow_cpp
If you git clone it, you will get this in the folder:
├── BUILD
├── CMakeLists.txt
├── data_set.cc
├── data_set.h
├── model.cc
├── normalized_car_features.csv
└── README.md
My CMakeLists.txt is here:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed
set(CMAKE_AUTOMOC ON)
# Create code from a list of Qt designer ui files
set(CMAKE_AUTOUIC ON)
project(DNN_Tensorflow_CPP LANGUAGES CXX)
add_executable(${PROJECT_NAME} model.cc data_set.cc data_set.h)
#target_link_libraries(main PRIVATE tensorflow)
configure_file(normalized_car_features.csv ${CMAKE_CURRENT_BINARY_DIR}/normalized_car_features.csv COPYONLY)
if(MSVC)
target_compile_definitions(main PRIVATE COMPILER_MSVC)
endif(MSVC)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -fPIC ")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} " )
include_directories("/usr/local/tensorflow/include/external/nsync/public")
include_directories("/usr/local/tensorflow/include/")
include_directories("/home/liudanny/git/tensorflow/tensorflow/contrib/makefile/downloads/absl")
include_directories("/home/liudanny/git/tensorflow")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} "/usr/local/tensorflow/lib/libtensorflow_cc.so")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} "/usr/local/tensorflow/lib/libtensorflow_framework.so")
Finally, I can build and run it successfully.
$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./DNN_Tensorflow_CPP
P.S: For the more in details about the C++ example, please check out this blog:
https://matrices.io/training-a-deep-neural-network-using-only-tensorflow-c/
P.S: If you encounter some errors, please check out the following link to seek for the solution:
http://www.liuxiao.org/2018/08/ubuntu-tensorflow-c-%E4%BB%8E%E8%AE%AD%E7%BB%83%E5%88%B0%E9%A2%84%E6%B5%8B1%EF%BC%9A%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/
No comments:
Post a Comment