# Building the C++ Library from Source Code

Before you begin, please select your operating system below.

We strongly recommend using the installer from the [tutorial](https://software.zaber.com/motion-library/docs/tutorials/install/cpp.md) unless you are familiar with CMake and the build tools of your platform.

> **Linux:**
>
> Use your distributions package manager to install GCC and CMake.
>
> - **GCC** should be version `11` or higher.
> - **CMake** should be version `3.22` or higher.
>
> Verify the installed versions in your terminal:
>
> ```bash
> g++ --version
> # example output:
> # gcc (GCC) 11.4.0
> # Copyright (C) 2021 Free Software Foundation, Inc.
> # This is free software; see the source for copying conditions.  There is NO
> # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
> cmake --version
> # example output:
> # cmake version 3.22.1
> # CMake suite maintained and supported by Kitware (kitware.com/cmake).
> ```

> **Mac OS:**
>
> Use the homebrew package manager to install clang (llvm) and CMake.
>
> - **llvm** should be version `14.0.0` or higher.
> - **CMake** should be version `3.19` or higher.
>
> ```bash
> brew install --with-toolchain llvm
> brew install cmake
> ```
>
> Verify the installed versions in your terminal:
>
> ```bash
> llvm-gcc --version
> # example output:
> # Apple clang version 14.0.3 (clang-1403.0.22.14.1)
> # Target: arm64-apple-darwin22.5.0
> # Thread model: posix
> # InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
>
> cmake --version
> # example output:
> # cmake version 3.19.2
> # CMake suite maintained and supported by Kitware (kitware.com/cmake).
> ```

> **Windows:**
>
> Ensure that you have [Microsoft Visual Studio](https://visualstudio.microsoft.com/vs/) installed.
> Install [CMake](https://cmake.org/download/). When installing CMake please ensure that you select the "Add to system path" option.
>
> - **Visual Studio** edition should be `2022` or later.
> - **CMake** should be version `3.15` or higher.
>
> Verify the installed versions in your command prompt:
>
> ```bash
> cmake --version
> # example output:
> # cmake version 3.15.0
> # CMake suite maintained and supported by Kitware (kitware.com/cmake).
> ```

Download the [Zaber Motion Library (ZML) source package](https://www.zaber.com/support/software-downloads.php?product=zml_cpp_src&version=latest).
Please note that the source package contains prebuilt Go shared library required by ZML.
If you decide to checkout the source code from the Git repository,
you will need to install the Go toolchain build the shared library yourself.

> **Windows:**
>
> Then open a command prompt and enter in the following commands.
> You will need to specify the system architecture when calling cmake, for most systems this is 64bit (`x64`).
> Note that if you wish to install the library (last step below) this command prompt must be started with admininstrator privileges.
>
> ```bash
> # In the same directory as your unzipped source directory
>
> # This path will change depending on the version of Visual Studio you have installed
> call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsMSBuildCmd.bat"
>
> cd ZaberMotionCppSource/cpp
> mkdir build
> cd build
> cmake -G "Visual Studio 17 2022" -A x64 ../ -DZML_ARCH=x64
> msbuild "Zaber Motion Library.sln" /p:Configuration=Release
> devenv "Zaber Motion Library.sln" /build Release /project INSTALL  # Will require administrator privileges
> ```
>
> The install step will place the libraries in `C:\Program Files\Zaber Motion Library\`.
>
> For PowerShell, the commands are the same expect initializing the Visual Studio environment.
>
> ```powershell
> # This path will change depending on the version of Visual Studio you have installed
> & 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1'
> ```
>
> ## MinGW
>
> While it is possible to build the library using MinGW compiler, we do not support this use case.
> Please take the following instructions without any warranty.
>
> In order to build the library the MinGW distribution must support dynamic linking (`-ldl`).
> This is true of [w64devkit](https://github.com/skeeto/w64devkit) but not of some other distributions.
> You may need to install some additional packages.
> Use the following commands to generate and build the project.
>
> ```shell
> cmake -G "MinGW Makefiles" ../ -DZML_ARCH=x64
> make
> ```
>
> You may need to change some of the source/generated files to achieve a working configuration.

> **Linux and Mac OS:**
>
> Then use CMake to build and install the source in your terminal.
> You will need to specify the system architecture when calling cmake, for most systems this is 64bit (`x64`).
>
> ```bash
> # In the same directory as your unzipped source directory
> cd ZaberMotionCppSource/cpp
> mkdir build && cd build
> cmake ../ -DZML_ARCH=x64
> make
> make install  # This may require root privileges
> ```
