Building MAVSDK Library from Source

This section explains how to build and install the MAVSDK C++ library from source for all our target platforms.

Note that there are separate instructions to build the mavsdk_server.

Build the C++ Library

This section explains how to build the library along with its unit and integration tests.

Requirements

The build requirements are git, cmake, and a compiler such as GCC, Clang, or MSVC.

Linux

Ubuntu:

sudo apt-get update
sudo apt-get install build-essential cmake git

Fedora:

sudo dnf update
sudo dnf groupinstall "Development Tools" "Development Libraries"
sudo dnf install cmake git

Arch Linux:

sudo pacman -Sy base-devel cmake git

macOS

First install XCode Command line tools:

xcode-select --install

And Homebrew (for cmake). Once you have installed brew, you can install cmake using brew in the terminal:

brew install cmake

Windows

To build the library in Windows, you need:

  • Build Tools for Visual Studio 2022: Download and install (only the "Visual C+ Build Tools" are needed from installer).
  • cmake: Download the installer and run it. Make sure to tick "add to PATH" during the installation.
  • git or any other tool to work with git.

Note: Building using Cygwin or MingW is not supported.

Getting the source

Download the source using git:

git clone https://github.com/mavlink/MAVSDK.git

Make sure to get all the submodules as well:

git submodule update --init --recursive

Building Fundamentals

Building the MAVSDK with CMake is broken down into two steps: Configuration and Build.

Build Configuration Step

For configuration, you specify the type of build you want to execute in the build step. You can checkout the CMake Documentation on Build Configuration for more context.

A typical configuration command example would be:

cmake -DCMAKE_BUILD_TYPE=Debug -Bbuild/default -H.

  • Build type is set to Debug
  • Build directory is set to build/default
  • Chose the directory using the H. flag

During the configure step you can set more flags using -DFLAG=Value:

  • CMAKE_BUILD_TYPE: as documented above, to chose between Debug and Release build.
  • CMAKE_INSTALL_PREFIX: as documented above, to specify directory to install library artifacts.
  • BUILD_SHARED_LIBS: set to ON to build dynamic libraries (such as .so on Linux, .dylib on macOS, .dll on Windows). Set to OFF to build static libraries (such as .a on Linux and macOS, .lib on Windows).
  • SUPERBUILD: set to OFF to use system dependencies instead of third party dependencies downloaded and built using cmake.
  • CMAKE_PREFIX_PATH: can be used to set the path where the dependencies can be found if SUPERBUILD is set to OFF.
  • BUILD_MAVSDK_SERVER: set to ON to build mavsdk_server, see instruction to build mavsdk_server.
  • ASAN: set to ON to enable address sanitizer.
  • UBSAN: set to ON to enable undefined behavior sanitizer.
  • LSAN: set to ON to enable leak sanitizer.
  • WERROR: set to ON to error on warnings, mostly used for CI.

After the configuration step, everything that will be build in the build step have been specified, and if you want to change your build configuration (e.g. If you want to build Debug build instead of Release build), you must execute the configuration step again!

Build Step

In the build step, we finally build the library and binaries and link them.

The stripped down version of the build command would be:

cmake --build build/default

  • The --build signals the CMake to execute the build
  • It uses the build configuration built from the configuration step, located at build/default folder

Additionally, you can install it in the system with the --target install command added as well, which is explained in detail in the Installing the C++ Library section below.

Building

Debug

To build the MAVSDK C++ Library for development, use the debug build.

There are 2 steps in building a library: Configure and build.

cmake -DCMAKE_BUILD_TYPE=Debug -Bbuild/default -H.
cmake --build build/default -j8

Release

Once you ship software, make sure to use the release build with optimizations turned on:

Linux/macOS:

 cmake -Bbuild/default -DCMAKE_BUILD_TYPE=Release -H.
 cmake --build build/default -j8

Windows:

cmake -Bbuild/default -H. -DCMAKE_BUILD_TYPE=Release
cmake --build build/default -j8 --config Release

Note: It is not usual to use CMAKE_BUILD_TYPE on Windows (with MSVC), however, our build requires it for the dependencies which are built at configure time.

Installing the C++ Library

Installing builds the SDK and copies the libraries and header files into a "public" location so that they can be referenced by C++ applications (see Building C++ Apps).

System-wide Install

You can configure to install system wide by not setting the CMAKE_INSTALL_PREFIX in the configuration step, since CMake tries to install system wide by default. For Linux/macOS that's /usr/local, for Windows it is somewhere in C:\Program Files.

To install system-wide the command needs to be run with sudo on Linux/macOS:

sudo cmake --build build/default --target install

or run in a command prompt with admin privileges on Windows, or using runas:

runas cmake --build build/default --target install

Make sure to prevent conflicts between libraries installed in your system. The mavsdk library installed via a .deb or .rpm file will be installed in /usr/ while the built library will be installed in /usr/local. It's recommended to only have one installed at any time to prevent conflicts.

Local Install

The install path can be set in the configuration step using CMAKE_INSTALL_PREFIX:

For example, to install into the MAVSDK/install/ folder you would set the CMAKE_INSTALL_PREFIX variable to specify a path relative to the folder from which you call cmake (or an absolute path).

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install -Bbuild/default -H.
cmake --build build/default --target install

If you already have run cmake without setting CMAKE_INSTALL_PREFIX, you may need to clean the build first:

  rm -rf build/default

Troubleshooting

Git submodules out of date

Some common build issues can be resolved by updating submodules and cleaning the distribution:

cd MAVSDK
git submodule update --recursive
rm -rf build

Then attempt to build again.

MSVC vs. MingW

We only try to support building using MSVC, MingW is not working, mostly because the defines/flags for it are missing.

Undefined reference to mavsdk...

If you only just built the library and installed it system-wide may also need to update the linker cache.

On Linux this is done with sudo ldconfig.

© MAVSDK Development Team 2017-2023. License: CC BY 4.0            Updated: 2023-12-27 03:58:12

results matching ""

    No results matching ""