Build Tensorflow c++ Static Libraries

Build static libraries:

1
2
3
sudo apt-get install autoconf automake libtool curl make g++ unzip zlib1g-dev git python
git clone https://github.com/tensorflow/tensorflow.git
source ./tensorflow/tensorflow/contrib/makefile/build_all_linux.sh

Heads and libs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
cd your_project
mkdir -p ./lib/tensorflow/include/tensorflow/contrib
mkdir -p ./lib/tensorflow/lib
mkdir -p ./lib/tensorflow/third_party

cp -r path_to_tensorflow/tensorflow/cc ./lib/tensorflow/include/tensorflow
cp -r path_to_tensorflow/tensorflow/core ./lib/tensorflow/include/tensorflow
cp -r path_to_tensorflow/tensorflow/contrib/cloud ./lib/tensorflow/include/tensorflow/contrib

cp path_to_tensorflow/tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a ./lib/tensorflow/lib

cp -r path_to_tensorflow/tensorflow/contrib/makefile/downloads/absl ./lib/tensorflow/third_party

cp -r path_to_tensorflow/tensorflow/contrib/makefile/gen/proto ./lib/tensorflow/third_party

mkdir -p ./lib/tensorflow/third_party/protobuf-host
cp -r path_to_tensorflow/tensorflow/contrib/makefile/gen/protobuf-host/include ./lib/tensorflow/third_party/protobuf-host
cp -r path_to_tensorflow/tensorflow/contrib/makefile/gen/protobuf-host/lib ./lib/tensorflow/third_party/protobuf-host

mkdir -p ./lib/tensorflow/third_party/nsync/builds
cp -r path_to_tensorflow/tensorflow/contrib/makefile/downloads/nsync/public ./lib/tensorflow/third_party/nsync
cp -r path_to_tensorflow/tensorflow/contrib/makefile/downloads/nsync/builds/default.linux.c++11 ./lib/tensorflow/third_party/nsync/builds

cp -r path_to_tensorflow/tensorflow/contrib/makefile/downloads/eigen ./lib/tensorflow/third_party
cp -r path_to_tensorflow/third_party/eigen3 ./lib/tensorflow/third_party

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
...
ET(LDFLAGS "-std=c++11 -fPIC -O3 -ggdb -Wall -finline-functions -Wl,--allow-multiple-definition -Wl,--whole-archive")
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LDFLAGS}")

set(TENSORFLOW_INCLUDES
${PROJECT_SOURCE_DIR}/lib/tensorflow
${PROJECT_SOURCE_DIR}/lib/tensorflow/include
${PROJECT_SOURCE_DIR}/lib/tensorflow/third_party/proto
${PROJECT_SOURCE_DIR}/lib/tensorflow/third_party/protobuf-host/include
${PROJECT_SOURCE_DIR}/lib/tensorflow/third_party/eigen
${PROJECT_SOURCE_DIR}/lib/tensorflow/third_party/nsync/public
${PROJECT_SOURCE_DIR}/lib/tensorflow/third_party/absl
)

set(TENSORFLOW_LIBS
${PROJECT_SOURCE_DIR}/lib/tensorflow/lib/libtensorflow-core.a
${PROJECT_SOURCE_DIR}/lib/tensorflow/third_party/protobuf-host/lib/libprotobuf.a
${PROJECT_SOURCE_DIR}/lib/tensorflow/third_party/nsync/builds/default.linux.c++11/nsync.a
pthread m z dl
)

...

If undefined reference to tensorflow::Status::ToString() const issue arise, try to build you project with -D_GLIBCXX_USE_CXX11_ABI=0 which will force GCC to use the old ABI version or try to use -D_GLIBCXX_USE_CXX11_ABI=1.

This static library will not contain: * Python or other language bindings * GPU support