Building SDK from Source
This section explains how to build and install the MAVSDK C++ library from source for all our target platforms. It also shows how to build the SDK with extensions and build the API Reference documentation.
Build the C++ Library
This section explains how to build the SDK C++ library from source, along with its unit and integration tests. Build artifacts are created in the build subdirectory.
macOS
First install:
- XCode (for clang)
- Homebrew (for cmake).
Once you have installed brew, you can install cmake via the terminal:
brew install cmake
Then follow the instructions for building the library on Linux.
Linux
To build the MAVSDK C++ Library on Linux (or macOS after installing the preconditions above):
First install the dependencies
sudo apt-get update -y sudo apt-get install cmake build-essential colordiff git doxygen -y
If the build reports a missing dependency, confirm that the set above matches the requirements in the appropriate docker file for your platform.
Clone the MAVSDK repository (or your fork):
git clone https://github.com/mavlink/MAVSDK.git cd MAVSDK
- Checkout the release/branch you want to build (the
develop
branch is checked out by default).- Latest stable build (
master
):git checkout master
- Head revision with latest features (
develop
):git checkout develop
- Latest stable build (
- Update the submodules:
git submodule update --init --recursive
- Build the (debug) C++ library by calling:
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=ON -Bbuild/default -H. cmake --build build/default
You can build release binaries by setting
CMAKE_BUILD_TYPE=Release
as shown below:cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -Bbuild/default -H. cmake --build build/default
- "Install" the MAVSDK as described below.
Windows
To build the library in Windows, you need:
- Build Tools for Visual Studio 2019: 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.
To build the MAVSDK C++ Library on Windows:
- Clone the MAVSDK repository (or your fork):
git clone https://github.com/mavlink/MAVSDK.git cd MAVSDK
- Checkout the release/branch you want to build (the
develop
branch is checked out by default).- Latest stable build (
master
):git checkout master
- Head revision with latest features (
develop
):git checkout develop
- Latest stable build (
- Update the submodules:
git submodule update --init --recursive
Then build the MAVSDK in Windows:
cd /your/path/to/MAVSDK cmake -G "Visual Studio 16 2019" -DBUILD_SHARED_LIBS=OFF -Bbuild/default -H. cmake --build build/default --config Debug
You can generate release binaries by setting
--config Release
in the build step:cmake --build build --config Release
Tip To use more than one CPU core to build set this before building:
set CL=/MP
"Install" the SDK as described below.
Install the SDK
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). The SDK supports installation system-wide by default. You can also install files locally/relative to the MAVSDK tree if needed.
System-wide installation is not yet supported on Windows (see #155) so you will need to install the SDK locally.
Windows gurus, we'd love your help to implement this).
System-wide Install
System-wide installation copies the SDK headers and binaries to the standard system-wide locations for your platform (e.g. on Ubuntu Linux this is /usr/local/).
System-wide installation overwrites any previously installed version of the SDK.
To install the SDK system-wide:
sudo cmake --build build/default --target install # sudo is required to install to system directories!
# First installation only
sudo ldconfig # update linker cache
The first time you build the SDK you may also need to update the linker cache. On Ubuntu this is done with
sudo ldconfig
, as shown above.
Local Install
Local installation copies the SDK headers/library to a user-specified location inside the SDK source directory.
On Linux/macOS use the CMAKE_INSTALL_PREFIX
variable to specify a path relative to the folder from which you call cmake
(or an absolute path).
For example, to install into the MAVSDK/install/ folder you would call:
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -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
Build for Android
Build for Android using the dockcross cross compiler, as described in the cross-compilation section below (e.g. with the dockcross/android-arm64
image).
Build for iOS
To build for real iOS devices on macOS:
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_BACKEND=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_TOOLCHAIN_FILE=tools/ios.toolchain.cmake -DPLATFORM=OS -Bbuild/ios -H.
cmake --build build/ios
Build for the iOS simulator on macOS:
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_BACKEND=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_TOOLCHAIN_FILE=tools/ios.toolchain.cmake -DPLATFORM=SIMULATOR64 -Bbuild/ios_simulator -H.
The commands above build the mavsdk_server (
-DBUILD_BACKEND=ON
). This is required for contributors who want to extend the MAVSDK programming-language-specific libraries like MAVSDK-Swift, but is not required for developers who just want to use C++, MAVSDK-Swift or MAVSDK-Python.
Build SDK Extensions
The MAVSDK can be extended with plugins and integration tests that are defined "out of tree".
These are declared inside a parallel directory that is included into the SDK at compile time (by specifying EXTERNAL_DIR
in the make
command).
The commands to build the SDK with an extension library are:
cmake -DEXTERNAL_DIR=relative_path_to_external_directory -Dbuild/default -H.
cmake --build build/default
See SDK Extensions for more information.
Building mavsdk_server
The MAVSDK programming-language-specific libraries (e.g. Swift, Python) share a common backend (called "mavsdk_server"). This may optionally be built as part of the C++ library.
The cmake configuration step additionally depends on the -DBUILD_BACKEND=ON
option.
Otherwise the build is exactly the same as usual.
When building mavsdk_server, we usually like to link all the dependencies statically, and therefore we set
-DBUILD_SHARED_LIBS=OFF
(or don't specify it, because the default isOFF
).
Ubuntu
To build mavsdk_server on Ubuntu:
- Setup the C++ Library on Linux
- Navigate into the SDK directory and build the project
cd MAVSDK cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_BACKEND=ON -Bbuild/default -H. cmake --build build/default
macOS
To build mavsdk_server on macOS:
- Setup the C++ Library on macOS
- Navigate into the SDK directory and build the project
cd MAVSDK cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_BACKEND=ON -Bbuild/default -H. cmake --build build/default
Windows
To build mavsdk_server on Windows:
- Setup the C++ Library on Windows
- Navigate into the SDK directory and build the project
cmake -G "Visual Studio 16 2019" -DBUILD_SHARED_LIBS=OFF -DBUILD_BACKEND=ON -Bbuild/default -H. cmake --build build/default
Cross compilation using dockcross
Cross compilation is usually the fastest way to compile for "embedded" platforms like the Raspberry Pi, BeagleBone Blue or Nvidia Jetson (i.e. typically faster than native compilation on device itself).
We recommend using dockcross, which is a very convenient tool for cross compilation based on docker (and which supports many platforms).
For example, use the commands below to build for armv7
:
- Make sure docker is installed on your system.
- Navigate into the SDK directory, and use the commands below:
cd MAVSDK docker run --rm dockcross/linux-armv7 > ./dockcross-linux-armv7 chmod +x ./dockcross-linux-armv7 ./dockcross-linux-armv7 cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_BACKEND=ON -DBUILD_SHARED_LIBS=OFF -Bbuild/linux-armv7 -S. ./dockcross-linux-armv7 cmake --build build/linux-armv7 -j 8
- If all goes well,
mavsdk_server
is built. You can check the file with:./dockcross-linux-armv7 file build/linux-armv7-release/src/backend/src/mavsdk_server build/linux-armv7-release/src/backend/src/mavsdk_server: ELF 32-bit LSB executable, ARM, EABI5 version 1 (GNU/Linux), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 4.10.8, not stripped
- The newly built binary can now be copied to the device:
cp build/linux-armv7/src/backend/src/mavsdk_server somewhere/else
The commands above build the mavsdk_server (
-DBUILD_BACKEND=ON
). This is required for contributors who want to extend the MAVSDK programming-language-specific libraries like MAVSDK-Swift. It is not required for developers who just want to use C++, MAVSDK-Swift or MAVSDK-Python.
Build API Reference Documentation
The C++ source code is annotated using comments using Doxygen syntax.
Extract the documentation to markdown files (one per class) on macOS or Linux using the commands:
rm -rf tools/docs # Remove previous docs
./tools/generate_docs.sh
The files are created in /install/docs/markdown.
Extracting the API reference does not yet work automatically on Windows.
The generate_docs.sh script builds the library, installs it locally to /install, and then uses DOxygen to create XML documentation in /install/docs/xml. The generate_markdown_from_doxygen_xml.py script is then run on all files in the /xml directory to generate markdown files in /install/docs/markdown.
Troubleshooting
The vast majority of 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.