Compiling the TensorFlow C++ library on Windows 11 requires navigating different challenges compared to macOS, mainly due to the differences in available development tools and the operating system's architecture. This guide walks you through compiling TensorFlow v2.15.0 and running a C++ example on Windows 11.
Before starting, ensure you have Python, Bazel, and other necessary tools installed. Here's how to set up your environment:
-
Install Python 3.11: Download the following installer from the official Python downloads page, install and follow the instructions. Make sure to check the "Add Python to PATH" option during installation. or add it manually from the system settings.
python --version
-
Install Microsoft Visual C++ Redistributable: Download and install the latest Visual C++ Redistributable from the Microsoft Download Center, or directly from here
-
Install Visual Studio Code (optional for easy development): Download and install Visual Studio Code from the official website
- Install the C/C++ extension from the Extensions view (
Ctrl+Shift+X
). - Install the C++ extension from the Extensions view (
Ctrl+Shift+X
). - Install the CMake Tools extension from the Extensions view (
Ctrl+Shift+X
). - Install Python extension from the Extensions view (
Ctrl+Shift+X
). - Install Jupyter Notebook extension from the Extensions view (
Ctrl+Shift+X
).
- Install the C/C++ extension from the Extensions view (
-
Install Bazel:
- Download Bazelisk from GitHub.
- Rename the downloaded file to
bazel.exe
and add it to your system's PATH. - Verify the installation by running:
bazel --version
- Bazelisk is a wrapper for Bazel that automatically downloads the correct version of Bazel for your project.
-
Install Git: If not already installed, download and install Git from Git for Windows, or directly from here.
With the prerequisites in place, you're ready to build TensorFlow:
-
Open a Command Prompt: Right-click on the Start menu, select "Command Prompt (Admin)" or "Windows PowerShell (Admin)", and navigate to your desired working directory.
-
Clone TensorFlow Repository:
git clone https://github.com/tensorflow/tensorflow.git cd tensorflow git checkout v2.15.0
-
Configure TensorFlow Build: TensorFlow's configuration on Windows will prompt you for various options, such as Python and CUDA paths. Run the configuration script:
.\configure # click Enter for all to accept the default values
Answer the prompts according to your setup. For a basic CPU-only build, you can decline CUDA support.
-
Build TensorFlow C++ Library: Use Bazel to build the TensorFlow C++ library:
bazel build --config=opt //tensorflow:tensorflow_cc.dll
This command compiles the TensorFlow C++ library (
tensorflow_cc.dll
) optimized for Windows.
After building TensorFlow, you need to collect the compiled libraries and header files:
-
Locate Compiled Libraries: Find
tensorflow_cc.dll
andtensorflow_framework.dll
in thebazel-bin\tensorflow
directory. Copy them to your project or a known location. -
Copy Header Files: TensorFlow header files are located within the source tree. Copy the entire
tensorflow\include
directory to your project or another known location for easy access.
Verify the setup by compiling a simple TensorFlow C++ example:
-
Create a C++ Example File: Save an example TensorFlow C++ file, such as
example_trainer.cc
, in your project directory. -
Compile Your C++ Example: Use Visual Studio's
cl.exe
or another C++ compiler that's compatible with your setup, ensuring to include the TensorFlow headers and link against the TensorFlow libraries. The exact command will vary based on your compiler and setup. -
Run Your Example: Execute the compiled program from the Command Prompt or PowerShell to see it in action.
- Install CMake:
- Download the latest CMake installer from the official website.
- Run the installer and follow the instructions.
- Add CMake to your system's PATH.
- Verify the installation by running:
cmake --version
- Install C++ Compiler:
- Install Visual Studio Build Tools:
- Download the Visual Studio Build Tools installer from the official website.
- Run the installer and select the "C++ build tools" workload.
- Follow the instructions to complete the installation.
- Verify the installation by running:
cl
- If the command is not recognized, ensure that the Visual Studio Build Tools are correctly installed and added to your system's PATH.
- If you have Visual Studio installed, you can use the Developer Command Prompt for Visual Studio to access the
cl
compiler. - If you have MinGW or another C++ compiler installed, you can use it instead of the Visual Studio Build Tools.
- Make sure to adjust the compiler and linker flags in your C++ build commands accordingly.
- For example, you might use
g++
as the compiler and-ltensorflow_cc -ltensorflow_framework
as the linker flags when compiling your C++ example.
- for C compiler check this link
- For Installing Bazelisk check this link
- For Building TensorFlow on Windows check this link
- For C++ API check this link
- check these commands here
- here
This guide has outlined the steps to build and use the TensorFlow C++ library on Windows 11, covering the setup of necessary tools, building TensorFlow from source, and compiling a simple C++ example to verify the installation.
This adjusted tutorial provides an overview for Windows 11 users, highlighting key differences like tool installation and build commands. Make sure to follow the linked official documentation for detailed steps on installing prerequisites like Bazel and Visual Studio Build Tools.