|
| 1 | +# Building MAVSDK on Linux |
| 2 | + |
| 3 | +This guide explains how to build MAVSDK from source on Linux systems. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +The build requirements are git, cmake, and a C++ compiler (GCC or Clang), and python. |
| 8 | + |
| 9 | +### Ubuntu |
| 10 | +```bash |
| 11 | +sudo apt-get update |
| 12 | +sudo apt-get install build-essential cmake git python3 python3-pip |
| 13 | +``` |
| 14 | + |
| 15 | +### Fedora |
| 16 | +```bash |
| 17 | +sudo dnf update |
| 18 | +sudo dnf groupinstall "Development Tools" "Development Libraries" |
| 19 | +sudo dnf install cmake git |
| 20 | +``` |
| 21 | + |
| 22 | +## Getting the Source |
| 23 | + |
| 24 | +Download the source using git: |
| 25 | +```bash |
| 26 | +git clone https://github.com/mavlink/MAVSDK.git |
| 27 | +cd MAVSDK |
| 28 | +git submodule update --init --recursive |
| 29 | +``` |
| 30 | + |
| 31 | +## Building the MAVSDK library on Linux |
| 32 | + |
| 33 | +### Debug Build |
| 34 | + |
| 35 | +For development, use the debug build: |
| 36 | +```bash |
| 37 | +cmake -DCMAKE_BUILD_TYPE=Debug -Bbuild -S. |
| 38 | +cmake --build build -j8 |
| 39 | +``` |
| 40 | + |
| 41 | +### Release Build |
| 42 | + |
| 43 | +For production use, build with optimizations enabled: |
| 44 | +```bash |
| 45 | +cmake -DCMAKE_BUILD_TYPE=Release -Bbuild -S. |
| 46 | +cmake --build build -j8 |
| 47 | +``` |
| 48 | + |
| 49 | +## Installation |
| 50 | + |
| 51 | +### System-wide Installation |
| 52 | + |
| 53 | +To install system-wide in `/usr/local`: |
| 54 | +```bash |
| 55 | +sudo cmake --build build --target install |
| 56 | +sudo ldconfig # Update linker cache |
| 57 | +``` |
| 58 | + |
| 59 | +### Local Installation |
| 60 | + |
| 61 | +To install to a custom location, e.g. `install` |
| 62 | + |
| 63 | +```bash |
| 64 | +cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install -Bbuild -S. |
| 65 | +cmake --build build --target install |
| 66 | +``` |
| 67 | + |
| 68 | +## Build mavsdk_server binary on Linux |
| 69 | + |
| 70 | +Language wrappers for MAVSDK other than C++ connect to the MAVSDK C++ core using gRPC. This gRPC server around the MAVSDK C++ library is called mavsdk_server (in the past it was referred to as the backend). |
| 71 | + |
| 72 | +For more information about the architecture, also see [how the auto-generation works](../contributing/autogen.md). |
| 73 | + |
| 74 | +In order to include the mavsdk_server in the build, add `-DBUILD_MAVSDK_SERVER=ON`: |
| 75 | + |
| 76 | +```bash |
| 77 | +cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_MAVSDK_SERVER=ON -Bbuild -S. |
| 78 | +cmake --build build -j8 |
| 79 | +``` |
| 80 | + |
| 81 | +## Build Options |
| 82 | + |
| 83 | +During the configure step you can set various flags using `-DFLAG=Value`: |
| 84 | + |
| 85 | +- `CMAKE_BUILD_TYPE`: Choose between `Debug`, `Release`, or `RelWithDebInfo` (optimizations and debug symbols) |
| 86 | +- `CMAKE_INSTALL_PREFIX`: Specify directory to install library artifacts |
| 87 | +- `BUILD_SHARED_LIBS`: Set to `ON` for dynamic libraries (.so), `OFF` for static libraries (.a) |
| 88 | +- `SUPERBUILD`: Set to `OFF` to use system dependencies instead of third party dependencies (see [Building without Superbuild](#building-without-superbuild)) |
| 89 | +- `CMAKE_PREFIX_PATH`: Set path where dependencies can be found if `SUPERBUILD` is `OFF` |
| 90 | +- `BUILD_MAVSDK_SERVER`: Set to `ON` to build mavsdk_server |
| 91 | +- `BUILD_WITHOUT_CURL`: Set to `ON` to build without CURL support |
| 92 | +- `ASAN`: Set to `ON` to enable address sanitizer |
| 93 | +- `UBSAN`: Set to `ON` to enable undefined behavior sanitizer |
| 94 | +- `LSAN`: Set to `ON` to enable leak sanitizer |
| 95 | +- `WERROR`: Set to `ON` to treat warnings as errors |
| 96 | + |
| 97 | +### MAVLink Configuration |
| 98 | + |
| 99 | +MAVSDK supports configuring the MAVLink dialect and repository: |
| 100 | + |
| 101 | +- `MAVLINK_DIALECT`: MAVLink dialect to use (default: `ardupilotmega`) |
| 102 | +- `MAVLINK_XML_PATH`: Path to local MAVLink XML definition files. If not set, definitions are fetched automatically. |
| 103 | +- `MAVLINK_URL`: URL for the MAVLink repository (default: `https://github.com/mavlink/mavlink`) |
| 104 | +- `MAVLINK_HASH`: Git commit hash to use from the MAVLink repository |
| 105 | + |
| 106 | +> **Note:** You can also load custom MAVLink message definitions at runtime using the [MavlinkDirect](../api_reference/classmavsdk_1_1_mavlink_direct.md) plugin, without needing to rebuild MAVSDK. |
| 107 | +
|
| 108 | +Examples: |
| 109 | + |
| 110 | +```bash |
| 111 | +# Build with development dialect |
| 112 | +cmake -DCMAKE_BUILD_TYPE=Debug -DMAVLINK_DIALECT=development -Bbuild -S. |
| 113 | + |
| 114 | +# Use a custom MAVLink repository fork |
| 115 | +cmake -DCMAKE_BUILD_TYPE=Debug \ |
| 116 | + -DMAVLINK_URL=https://github.com/yourfork/mavlink \ |
| 117 | + -DMAVLINK_HASH=abc123def456 \ |
| 118 | + -Bbuild -S. |
| 119 | + |
| 120 | +# Use local MAVLink XML files |
| 121 | +cmake -DCMAKE_BUILD_TYPE=Debug \ |
| 122 | + -DMAVLINK_XML_PATH=/path/to/mavlink/message_definitions/v1.0 \ |
| 123 | + -Bbuild -S. |
| 124 | +``` |
| 125 | + |
| 126 | +## Building without Superbuild |
| 127 | + |
| 128 | +By default, MAVSDK uses a "superbuild" that automatically downloads and builds all required dependencies. If you prefer to provide dependencies yourself (e.g., from system packages or custom builds), you can disable this with `SUPERBUILD=OFF`. |
| 129 | + |
| 130 | +A script is provided that demonstrates how to build all dependencies and MAVSDK: |
| 131 | + |
| 132 | +```bash |
| 133 | +./tools/build-with-system-deps.sh |
| 134 | +``` |
| 135 | + |
| 136 | +This script: |
| 137 | +1. Clones and builds the required dependencies (MAVLink, libevents, PicoSHA2, libmav) into a local `deps/` directory |
| 138 | +2. Installs them to `deps-install/` |
| 139 | +3. Builds MAVSDK with `SUPERBUILD=OFF` using these dependencies |
| 140 | + |
| 141 | +Prerequisites (install before running the script): |
| 142 | +```bash |
| 143 | +sudo apt install build-essential cmake git python3 python3-pip \ |
| 144 | + liblzma-dev libtinyxml2-dev libjsoncpp-dev \ |
| 145 | + libcurl4-openssl-dev libssl-dev |
| 146 | +``` |
| 147 | + |
| 148 | +## Troubleshooting |
| 149 | + |
| 150 | +### Git Submodules Out of Date |
| 151 | + |
| 152 | +If you encounter build issues, try updating the submodules: |
| 153 | +```bash |
| 154 | +git submodule update --recursive |
| 155 | +``` |
| 156 | + |
| 157 | +### Undefined References |
| 158 | + |
| 159 | +If you get undefined reference errors after installation, update the linker cache: |
| 160 | +```bash |
| 161 | +sudo ldconfig |
| 162 | +``` |
| 163 | + |
| 164 | +And try a clean build by wiping the build directory: |
| 165 | +```bash |
| 166 | +rm -r build |
| 167 | +``` |
0 commit comments