A small cross-platform set of TensorFlow C API examples for Windows, Linux, and macOS.
- CMake 3.20 or newer.
- C++17 compiler.
- Python with pip. CI uses Python 3.12.
- 64-bit target platform.
- Hello TF
- Load graph
- Create Tensor
- Create String Tensor
- Image processing
- Run target operation
- OpenCV image file processing (optional, requires OpenCV)
- Allocate Tensor
- Run session
- Repeated inference
- Interface
- Batch Interface
- Tensor Info
- Graph Info
git clone --depth 1 https://github.com/Neargye/hello_tf_c_api
cd hello_tf_c_api
mkdir build
cd build
cmake -A x64 ..
cmake --build . --config Release
ctest --output-on-failure -C Release
git clone --depth 1 https://github.com/Neargye/hello_tf_c_api
cd hello_tf_c_api
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . -j 4
ctest --output-on-failure
git clone --depth 1 https://github.com/Neargye/hello_tf_c_api
cd hello_tf_c_api
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --config Release
ctest --output-on-failure -C Release
- CMake downloads TensorFlow 2.21.0 from the Python wheel into the build-local
<build>/_deps/tensorflow/pythoncache by default. - To use an existing local TensorFlow wheel extraction, configure with
-DTENSORFLOW_ROOT=/path/to/tensorflow. Auto-fetch only writes to the default build-local TensorFlow cache; it refuses to overwrite an externalTENSORFLOW_ROOT. To require a pre-existing extraction and disable downloads during configure, add-DHELLO_TF_FETCH_TENSORFLOW=OFF. - Python with pip is required during CMake configure.
- The small GraphDef used by graph and session examples is committed as
models/graph.pb; no external model download is required. - To regenerate the example GraphDef, run
python tools/create_example_graph.pyfrom a Python environment where the full TensorFlow package is available. - OpenCV is optional. If CMake finds it, the OpenCV image-file example is built and tested.
- On Windows, CMake copies the required TensorFlow runtime DLLs into the build output directories.
- Tests use doctest. CI also runs an ASan/UBSan test job on Ubuntu.
- To configure only the helper library without example executables, add
-DHELLO_TF_BUILD_EXAMPLES=OFF. - Tests follow CMake's standard
BUILD_TESTINGoption. To configure without tests, add-DBUILD_TESTING=OFF.
This project uses the TensorFlow 2.21.0 Python wheel and links the C API headers and native libraries from the local <TENSORFLOW_ROOT>/python directory. The CMake file creates an imported tensorflow target, a hello_tf_utils helper library target, and copies required runtime libraries where needed.
tf_utils::LoadGraph only imports a GraphDef. If a graph needs checkpoint restore operations, create the session first and call tf_utils::RestoreCheckpoint(session, graph, ...) on that session. TensorFlow variable state belongs to TF_Session, not to TF_Graph.
If you want to link TensorFlow manually, use the headers from:
<TENSORFLOW_ROOT>/python/tensorflow/include
and the native libraries from:
<TENSORFLOW_ROOT>/python/tensorflow
<TENSORFLOW_ROOT>/python/tensorflow/python
You can also build the TensorFlow library version you need from source, with CPU or GPU support.
Examples that use the helper API link the hello_tf_utils target:
target_link_libraries(<target> PRIVATE hello_tf_utils)
Examples that demonstrate only the raw TensorFlow C API use:
target_link_tensorflow(<target>)
If another project needs a small part of this repository, copy the relevant example or helper source and wire it to that project's TensorFlow target explicitly. This repository is maintained as local examples plus tests, not as a packaged dependency.
Open "Project" -> "Properties" -> "Configuration Properties" -> "C/C++" -> "Additional Include Directories" and add the TensorFlow include path.
Open "Project" -> "Properties" -> "Configuration Properties" -> "Linker" -> "Additional Dependencies" and add the TensorFlow import library path.
Make sure that the TensorFlow DLLs are in the output directory or in a directory contained by the %PATH% environment variable.
This repository already includes the demo models/graph.pb used by the examples. For your own models, prefer a TensorFlow 2 SavedModel export, or use a small inference-only GraphDef when you want the same import path as these examples.
- https://www.tensorflow.org/guide/saved_model
- https://www.tensorflow.org/lite/performance/model_optimization
