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):

  1. 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.

  2. Clone the MAVSDK repository (or your fork):

    git clone https://github.com/mavlink/MAVSDK.git
    cd MAVSDK
    
  3. 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
      
  4. Update the submodules:
    git submodule update --init --recursive
    
  5. 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
    
  6. "Install" the MAVSDK as described below.

Windows

To build the library in Windows, you need:

  • Build Tools for Visual Studio 2017: 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:

  1. Clone the MAVSDK repository (or your fork):
    git clone https://github.com/mavlink/MAVSDK.git
    cd MAVSDK
    
  2. 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
      
  3. Update the submodules:
    git submodule update --init --recursive
    
  4. Then build the MAVSDK in Windows:

    cd /your/path/to/MAVSDK
    cmake -G "Visual Studio 15 2017" -DBUILD_SHARED_LIBS=ON -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
    
  5. "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

You must first build the C++ Library (as shown above).

To build for Android devices or simulators, you first need to install:

Also, you need to set these three environment variables:

  • NDK_ROOT to <your-android-ndk>
  • ANDROID_TOOLCHAIN_CMAKE to <your-android-ndk>/build/cmake/android.toolchain.cmake
  • ANDROID_CMAKE_BIN to <your-android-sdk>/cmake/<version>/bin/cmake

E.g. you can add the lines below to your .bashrc, (or .zshrc for zshell users, or .profile):

export NDK_ROOT=$HOME/Android/android-ndk-r13
export ANDROID_TOOLCHAIN_CMAKE=$HOME/Android/android-ndk-r13/build/cmake/android.toolchain.cmake
export ANDROID_CMAKE_BIN=$HOME/Android/Sdk/cmake/3.6.3155560/bin/cmake

Then you build for all Android architectures:

make android install

Build for iOS

You must first build the C++ Library (as shown above).

To build for real iOS devices on macOS:

cmake -DCMAKE_TOOLCHAIN_FILE=tools/ios.toolchain.cmake -Bbuild/ios -H.
cmake --build build/ios

Build for the iOS simulator on macOS:

cmake -DCMAKE_TOOLCHAIN_FILE=tools/ios.toolchain.cmake -DPLATFORM=SIMULATOR64 -Bbuild/ios_simulator -H.

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 the Backend

The MAVSDK programming-language-specific libraries (e.g. Swift, Python) share a common backend, which 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 the backend, 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 is OFF).

Ubuntu

To build the backend on Ubuntu:

  1. Setup the C++ Library on Linux
  2. Navigate into the SDK directory and build the project
    cd MAVSDK
    cmake -DBUILD_BACKEND=ON -Bbuild/default -H.
    cmake --build build/default
    

macOS

To build the backend on macOS:

  1. Setup the C++ Library on macOS
  2. Navigate into the SDK directory and build the project
    cd MAVSDK
    cmake -DBUILD_BACKEND=ON -Bbuild/default -H.
    cmake --build build/default
    

Windows

To build the backend on Windows:

  1. [Setup the C++ Library on Windows(#build_cpp_windows)
  2. Navigate into the SDK directory and build the project
    cmake -G "Visual Studio 15 2017" -DBUILD_BACKEND=ON -Bbuild/default -H.
    cmake --build build/default
    

Cross compilation using dockcross

To compile for platforms like the Raspberry Pi, BeagleBone Blue or Nvidia Jetson, cross compilation is typically faster than native compilation on the "embedded" device.

We recommend to use dockcross which is a very convenient tool for cross compilation based on docker supporting many platforms.

For example, use the commands below to build for armv7:

  1. Make sure docker is installed on your system.
  2. 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
    
  3. 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
    
  4. This built binary can now be copied to the device:
    cp build/linux-armv7/src/backend/src/mavsdk_server somewhere/else
    

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.

© Dronecode 2017-2019. License: CC BY 4.0            Updated: 2019-11-05 22:22:21

results matching ""

    No results matching ""