Back to Tensorflow

you may not use this file except in compliance with the License.

site/en/install/lang_c.ipynb

latest7.4 KB
Original Source
Copyright 2018 The TensorFlow Authors.
#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Install TensorFlow for C

<table class="tfo-notebook-buttons" align="left"> <td> <a target="_blank" href="https://www.tensorflow.org/install/lang_c">View on TensorFlow.org</a> </td> <td> <a target="_blank" href="https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/install/lang_c.ipynb">Run in Google Colab</a> </td> <td> <a target="_blank" href="https://github.com/tensorflow/docs/blob/master/site/en/install/lang_c.ipynb">View source on GitHub</a> </td> <td> <a href="https://storage.googleapis.com/tensorflow_docs/docs/site/en/install/lang_c.ipynb">Download notebook</a> </td> </table>

TensorFlow provides a C API that can be used to build bindings for other languages. The API is defined in <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api.h" class="external"><code>c_api.h</code></a> and designed for simplicity and uniformity rather than convenience.

Nightly libtensorflow C packages

libtensorflow packages are built nightly and uploaded to GCS for all supported platforms. They are uploaded to the libtensorflow-nightly GCS bucket and are indexed by operating system and date built. For MacOS and Linux shared objects, there is a script that renames the .so files versioned to the current date copied into the directory with the artifacts.

Supported Platforms

TensorFlow for C is supported on the following systems:

  • Linux, 64-bit, x86
  • macOS, Version 10.12.6 (Sierra) or higher
  • Windows, 64-bit x86

Setup

Download and extract

<table> <tr><th>TensorFlow C library</th><th>URL</th></tr> <tr class="alt"><td colspan="2">Linux <aside class="caution"> <p><b>Caution:</b> TensorFlow 2.16 was the last TensorFlow release that supported macOS x86.</p> <p><b>Caution:</b> TensorFlow 2.18 was the last release of Linux x86 libtensorflow packages.</p> </aside> </td></tr> <tr> <td>Linux CPU only</td> <td class="devsite-click-to-copy"><a href="https://storage.googleapis.com/tensorflow/versions/2.18.0/libtensorflow-cpu-linux-x86_64.tar.gz">https://storage.googleapis.com/tensorflow/versions/2.18.0/libtensorflow-cpu-linux-x86_64.tar.gz</a></td> </tr> <tr> <td>Linux GPU support</td> <td class="devsite-click-to-copy"><a href="https://storage.googleapis.com/tensorflow/versions/2.18.0/libtensorflow-gpu-linux-x86_64.tar.gz">https://storage.googleapis.com/tensorflow/versions/2.18.0/libtensorflow-gpu-linux-x86_64.tar.gz</a></td> </tr> <tr class="alt"><td colspan="2">macOS <aside class="caution"> <p><b>Caution:</b> TensorFlow 2.16 was the last TensorFlow release that supported macOS x86.</p> <p><b>Caution:</b> TensorFlow 2.18 was the last release of Mac Arm64 Libtensorflow packages.</p> </aside> </td></tr> <tr> <td>macOS CPU only</td> <td class="devsite-click-to-copy"><a href="https://storage.googleapis.com/tensorflow/versions/2.16.2/libtensorflow-cpu-darwin-x86_64.tar.gz">https://storage.googleapis.com/tensorflow/versions/2.16.2/libtensorflow-cpu-darwin-x86_64.tar.gz</a></td> </tr> <td>macOS ARM64 CPU only</td> <td class="devsite-click-to-copy"><a href="https://storage.googleapis.com/tensorflow/versions/2.18.0/libtensorflow-cpu-darwin-arm64.tar.gz">https://storage.googleapis.com/tensorflow/versions/2.18.0/libtensorflow-cpu-darwin-arm64.tar.gz</a></td> </tr> <tr class="alt"><td colspan="2">Windows <aside class="caution"> <p><b>Caution:</b> TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows.</p> <p><b>Caution:</b> TensorFlow 2.18 was the last release of Windows x86 libtensorflow packages.</p> </aside> </td></tr> <tr> <td>Windows CPU only</td> <td class="devsite-click-to-copy"><a href="https://storage.googleapis.com/tensorflow/versions/2.18.1/libtensorflow-cpu-windows-x86_64.zip">https://storage.googleapis.com/tensorflow/versions/2.18.1/libtensorflow-cpu-windows-x86_64.zip</a></td> </tr> <tr> <td>Windows GPU only</td> <td class="devsite-click-to-copy"><a href="https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-2.10.0.zip">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-2.10.0.zip</a></td> </tr> </table>

Extract the downloaded archive, which contains the header files to include in your C program and the shared libraries to link against.

On Linux and macOS, you may want to extract to /usr/local/lib:

%%bash
FILENAME=libtensorflow-cpu-linux-x86_64.tar.gz
wget -q --no-check-certificate https://storage.googleapis.com/tensorflow/versions/2.18.1/${FILENAME}
sudo tar -C /usr/local -xzf ${FILENAME}

Linker

On Linux/macOS, if you extract the TensorFlow C library to a system directory, such as /usr/local, configure the linker with ldconfig:

%%bash
sudo ldconfig /usr/local/lib

If you extract the TensorFlow C library to a non-system directory, such as ~/mydir, then configure the linker environmental variables:

<div class="ds-selector-tabs"> <section> <h3>Linux</h3> <pre class="prettyprint lang-bsh"> export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib </pre> </section> <section> <h3>macOS</h3> <pre class="prettyprint lang-bsh"> export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib </pre> </section> </div><!--/ds-selector-tabs-->

Build

Example program

With the TensorFlow C library installed, create an example program with the following source code (hello_tf.c):

%%writefile hello_tf.c
#include <stdio.h>
#include <tensorflow/c/c_api.h>

int main() {
  printf("Hello from TensorFlow C library version %s\n", TF_Version());
  return 0;
}

Compile

Compile the example program to create an executable, then run:

%%bash
gcc hello_tf.c -ltensorflow -o hello_tf

./hello_tf

Success: The TensorFlow C library is configured.

If the program doesn't build, make sure that gcc can access the TensorFlow C library. If extracted to /usr/local, explicitly pass the library location to the compiler:

%%bash
gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf

./hello_tf

Build from source

TensorFlow is open source. Read the instructions to build TensorFlow's C library from source code.