PX4 MAVSDK – C++ Programming [Part 3] Installing MAVSDK C++ and Setting Up the SITL Simulation Environment
Hello! This is QUAD Drone Lab. In our previous post (Part 2), we explored the core C++ syntax essential for handling MAVSDK C++, including pointers, references, lambda functions, and templates.
Having solidified our theoretical background, it is now time to create the “playground” where our code will actually run. Testing drone control code developed in a lab directly on a physical aircraft is extremely dangerous. A single typo or a minor logic error can lead to a crash costing thousands of dollars or, worse, serious personal injury.
This is why robotics researchers and university students worldwide first validate their code perfectly in a virtual simulation environment called SITL (Software In The Loop). In this third installment, we will provide a very friendly and detailed guide on how to integrate PX4 SITL with the Gazebo simulator for safe autonomous flight development, as well as the installation process for the MAVSDK C++ library.

What is SITL (Software In The Loop) Simulation?
SITL literally means the “software is in the loop.” It refers to a method where the PX4 flight stack (firmware) runs directly on a computer’s processor without actual drone hardware like flight controllers, sensors, or motors.

- Physical Environment Simulation: Gravity, wind, and sensor data (GPS, IMU, etc.) in the virtual environment are provided by powerful 3D physics simulators such as Gazebo or jMAVSim.
- Perfect Code Reusability: Amazingly, the MAVSDK C++ code tested in an SITL environment can be ported to a drone’s companion computer (Raspberry Pi, NVIDIA Jetson, etc.) and run identically without modifying a single line of code.
- Understanding Communication Ports: While physical drones communicate via serial (UART) ports or wireless telemetry, SITL simulations use virtual network ports within the computer. Typically, UDP port 14540 is the standard port assigned for MAVSDK Offboard control.
Installing the MAVSDK C++ Library (Source Build Recommended)
There are two main ways to install the MAVSDK C++ library: installing pre-built packages (.deb) or downloading and building the source code directly.
For graduate students and researchers, we strongly recommend the Build from Source method, as it allows you to use the latest features and understand the library’s internal structure. This guide is based on Ubuntu 22.04 LTS, the recommended OS.
Step 1: Download the Source Code
Open a terminal and use git to fetch the MAVSDK source code. The --recursive option is essential to include all necessary submodules.
git clone https://github.com/mavlink/MAVSDK.git
cd MAVSDK
git submodule update --init --recursive
Step 2: Configure CMake and Build Options
MAVSDK uses CMake, a cross-platform build system. Depending on your research goals, you can choose between a Debug build (includes debugging symbols) or a Release build (optimized for performance).
[Option A: Debug Build for Development and Testing]
Use this when you need to track errors and verify internal behavior.
cmake -DCMAKE_BUILD_TYPE=Debug -Bbuild/default -H.
[Option B: Release Build for Actual Flight and Optimization]
Use this for production environments or when running heavy algorithms that require maximum performance.
cmake -DCMAKE_BUILD_TYPE=Release -Bbuild/default -H.
💡 Build Options Tip for Researchers:
You can add various flags during CMake configuration:
-DBUILD_SHARED_LIBS=ON: Builds as dynamic libraries (.so). (If OFF, it builds as static libraries .a).-DBUILD_MAVSDK_SERVER=ON: Builds the gRPC backend server (mavsdk_server), which allows wrappers in other languages (Python, Swift, etc.) to access the MAVSDK C++ core.
Step 3: Compile and Install
Now, compile and install the library based on your configuration. The -j8 option instructs the system to build in parallel using 8 CPU cores; adjust this number according to your PC’s
# Proceed with compilation (This may take some time)
cmake --build build/default -j8
# Install libraries and header files system-wide (/usr/local)
sudo cmake --build build/default --target installNow you can use MAVSDK across your entire Ubuntu system by simply including #include <mavsdk/mavsdk.h>!
Running the PX4 SITL Simulator (Gazebo)
PX4 MAVSDK – C++ Programming [Part 3] Installing MAVSDK C++ and Setting Up the SITL Simulation Environment
Hello! This is QUAD Drone Lab. In our previous post (Part 2), we explored the core C++ syntax essential for handling MAVSDK C++, including pointers, references, lambda functions, and templates.
Having solidified our theoretical background, it is now time to create the “playground” where our code will actually run. Testing drone control code developed in a lab directly on a physical aircraft is extremely dangerous. A single typo or a minor logic error can lead to a crash costing thousands of dollars or, worse, serious personal injury.
This is why robotics researchers and university students worldwide first validate their code perfectly in a virtual simulation environment called SITL (Software In The Loop). In this third installment, we will provide a very friendly and detailed guide on how to integrate PX4 SITL with the Gazebo simulator for safe autonomous flight development, as well as the installation process for the MAVSDK C++ library.
What is SITL (Software In The Loop) Simulation?
SITL literally means the “software is in the loop.” It refers to a method where the PX4 flight stack (firmware) runs directly on a computer’s processor without actual drone hardware like flight controllers, sensors, or motors.
- Physical Environment Simulation: Gravity, wind, and sensor data (GPS, IMU, etc.) in the virtual environment are provided by powerful 3D physics simulators such as Gazebo or jMAVSim.
- Perfect Code Reusability: Amazingly, the MAVSDK C++ code tested in an SITL environment can be ported to a drone’s companion computer (Raspberry Pi, NVIDIA Jetson, etc.) and run identically without modifying a single line of code.
- Understanding Communication Ports: While physical drones communicate via serial (UART) ports or wireless telemetry, SITL simulations use virtual network ports within the computer. Typically, UDP port 14540 is the standard port assigned for MAVSDK Offboard control.
Installing the MAVSDK C++ Library (Source Build Recommended)
There are two main ways to install the MAVSDK C++ library: installing pre-built packages (.deb) or downloading and building the source code directly.
For graduate students and researchers, we strongly recommend the Build from Source method, as it allows you to use the latest features and understand the library’s internal structure. This guide is based on Ubuntu 22.04 LTS, the recommended OS.
Step 1: Download the Source Code
Open a terminal and use git to fetch the MAVSDK source code. The --recursive option is essential to include all necessary submodules.
Bash
git clone https://github.com/mavlink/MAVSDK.git
cd MAVSDK
git submodule update --init --recursive
Step 2: Configure CMake and Build Options
MAVSDK uses CMake, a cross-platform build system. Depending on your research goals, you can choose between a Debug build (includes debugging symbols) or a Release build (optimized for performance).
[Option A: Debug Build for Development and Testing] Use this when you need to track errors and verify internal behavior.
Bash
cmake -DCMAKE_BUILD_TYPE=Debug -Bbuild/default -H.
[Option B: Release Build for Actual Flight and Optimization] Use this for production environments or when running heavy algorithms that require maximum performance.
Bash
cmake -DCMAKE_BUILD_TYPE=Release -Bbuild/default -H.
💡 Build Options Tip for Researchers: You can add various flags during CMake configuration:
-DBUILD_SHARED_LIBS=ON: Builds as dynamic libraries (.so). (If OFF, it builds as static libraries .a).-DBUILD_MAVSDK_SERVER=ON: Builds the gRPC backend server (mavsdk_server), which allows wrappers in other languages (Python, Swift, etc.) to access the MAVSDK C++ core.
Step 3: Compile and Install
Now, compile and install the library based on your configuration. The -j8 option instructs the system to build in parallel using 8 CPU cores; adjust this number according to your PC’s specifications.
Bash
# Proceed with compilation (This may take some time)
cmake --build build/default -j8
# Install libraries and header files system-wide (/usr/local)
sudo cmake --build build/default --target install
Now you can use MAVSDK across your entire Ubuntu system by simply including #include <mavsdk/mavsdk.h>!
Running the PX4 SITL Simulator (Gazebo)
Let’s launch the “virtual drone” that our MAVSDK app will command. This assumes you already have the PX4 source code downloaded (if not, please clone the official PX4-Autopilot repository).
Open a terminal, navigate to your PX4 source directory, and run the following command:
cd PX4-Autopilot
make px4_sitl gazebo
Once entered, you will see the PX4 firmware booting logs in the terminal. Shortly after, the Gazebo 3D simulator window will open, showing a virtual quadcopter drone waiting on the runway.

This simulator behaves just like a real drone: the propellers spin when armed, and it is subject to physics—meaning it can be buffeted by wind or crash if it hits a wall. Our “safe laboratory” is now fully prepared.
MAVSDK Example App Build and Takeoff & Land Integration
With the simulator running, open a new terminal and execute the “Takeoff and Land” example included in the MAVSDK source code.
1. Navigate to the Example Directory and Build
# Move to the example folder within the MAVSDK directory
cd ~/MAVSDK/examples/takeoff_and_land/
# Generate build files with CMake
cmake -Bbuild -S.
# Compile the example code
cmake --build build -j42. Run the Example by Connecting to the Simulator
Once compilation is complete, an executable will be created in the build folder. Run it by passing the drone’s connection address udpin://0.0.0.0:14540 as an argument.
./build/takeoff_and_land udpin://0.0.0.0:14540

Workflow Analysis:
- The terminal displays connection success and telemetry data reception logs.
- The drone automatically checks its system status (GPS lock, etc.).
- The Action::arm() command starts the motors, and the propellers begin to spin in Gazebo.
- The Action::takeoff() command is sent, and the drone rises vertically to about 2–5 meters to hover stably.
- After a programmed delay, the Action::land() command brings the drone safely back to its original position, and the motors disarm.
Today, we successfully cleared the first hurdles of autonomous flight research: setting up the PX4 SITL virtual environment, building the MAVSDK C++ library from source, and controlling a virtual drone’s takeoff and landing.
We have now acquired a powerful “weapon” that allows us to experiment with complex algorithms and formulas without worrying about the costs of physical repairs.
In our next post, [Part 4: Building C++ Apps and Communication Connectivity], we will move beyond running existing examples. We will learn how to set up your own MAVSDK project from scratch by writing a CMakeLists.txt and establishing deep communication with the drone.
YouTube Tutorial

Author: Aiden, Marketing Team at QUAD Drone Lab
DATE: March 13, 2026
