Meson is a modern open-source build system designed to replace traditional tools like
MakeandCMake. It can be used in C++ projects, but it is not a “C++ library” itself.
🛠️ Build Your C++ Project with Meson: Simple, Fast, Modern
Have you ever experienced the following pain points?
- Writing
Makefileuntil your head hurts? - Finding
CMakeLists.txtincreasingly complex, like writing a “configuration language”? - Wanting to compile cross-platform but encountering a pile of configuration errors?
Don’t worry! Today we will introduce a modern, concise, and efficient build system — Meson.
🔍 What is Meson?
Meson is an open-source, high-performance build system designed for modern development. It uses simple and readable meson.build files to describe project structure and supports multiple languages including C, C++, Rust, and Python.
Its core features include:
| Feature | Description |
|---|---|
| ✅ Minimal Syntax | No more “nested function calls”, but a clear DSL |
| ⚡ Extremely Fast Compilation | Uses Ninja as the backend for rapid incremental builds |
| 🖥️ Cross-Platform | Supports Linux, Windows, macOS |
| 🔌 Rich Plugin Ecosystem | Automatically detects compilers, dependency libraries, and testing frameworks |
Official website: https://mesonbuild.com
🚀 Step 1: Install Meson
On Ubuntu/Debian:
sudo apt install meson ninja-build
On macOS (using Homebrew):
brew install meson ninja
On Windows (using Python pip):
pip install meson ninja
✅ Make sure you also have a C++ compiler (like
g++orclang++)
💡 Your First C++ Project: Hello World
Let’s create the simplest C++ project and build it with Meson.
1️⃣ Project Structure
hello_project/
├── meson.build
└── main.cpp
2️⃣ Write Code: main.cpp
#include <iostream>
int main() {
std::cout << "Hello from Meson! 🚀" << std::endl;
return 0;
}
3️⃣ Configure Build: meson.build
# Project name + language
project('hello', 'cpp')
# Define an executable named hello, built from main.cpp
executable('hello', 'main.cpp')
It’s that simple! No need to write compiler options or linker commands; Meson handles it automatically.
🔧 Compile and Run
# 1. Create a build directory (recommended practice)
meson setup builddir
# 2. Enter the directory and compile
cd builddir
ninja
# 3. Run the program
./hello
Output:
Hello from Meson! 🚀
🎉 Done! The entire process takes less than a minute.
🧩 Advanced Example: Using External Dependencies (Boost)
Suppose you want to write a more complex program using the Boost library.
Project Structure
boost_project/
├── meson.build
└── main.cpp
main.cpp: Using Boost.Optional
#include <boost/optional.hpp>
#include <iostream>
int main() {
boost::optional<int> value = 42;
if (value) {
std::cout << "Value is: " << *value << std::endl;
}
return 0;
}
meson.build: Automatically Find Boost
# Project name + C++ language
project('boost_hello', 'cpp')
# Find Boost dependency
boost_dep = dependency('boost', modules: ['system', 'optional'])
# Build executable and link Boost
executable('myapp', 'main.cpp', dependencies: boost_dep)
As long as Boost is installed on the system (
sudo apt install libboost-dev), Meson will automatically find and configure it.
Compile:
meson setup builddir
cd builddir
ninja
./myapp
Output:
Value is: 42
🏗️ Common Configuration Tips
✅ Specify C++ Standard
project('myapp', 'cpp', default_options: ['cpp_std=c++17'])
✅ Build Static/Shared Libraries
# Build a static library
static_lib = static_library('math', 'math.cpp')
# Executable links to it
executable('app', 'main.cpp', link_with: static_lib)
✅ Add Compiler Options
add_global_arguments('-Wall', '-Wextra', language: 'cpp')
✅ Unit Testing
test('test_hello', executable('test', 'test.cpp'))
Run tests:
meson test -C builddir
🆚 Meson vs CMake: A Comparison
| Feature | Meson | CMake |
|---|---|---|
| Syntax | Concise, readable | Complex, highly nested |
| Build Speed | Extremely fast (native support for Ninja) | Fast |
| Cross-Platform | Good | Very good |
| Dependency Management | Automatic detection (Pkg-config, CMake, etc.) | Powerful but complex |
| Learning Curve | Low | Medium to high |
💬 Many people say: “Once you use Meson, you never want to write CMake again.”
🌐 Real-World Applications
- GNOME Project: GTK, GIMP, and others use Meson
- High-Performance Computing: LLVM subprojects also support Meson
- Embedded Development: Very convenient with cross-compilation toolchains
- CI/CD Automation: Scripts are concise and easy to integrate
📚 Learning Resources
- Official Tutorial: https://mesonbuild.com/Tutorial.html
- Build File Reference: https://mesonbuild.com/Reference-manual.html
- GitHub Examples: https://github.com/mesonbuild/meson/tree/master/test%20cases
✅ Conclusion
Although Meson is not a C++ library, it is a modern build tool that can significantly enhance C++ development efficiency.
✅ Simple syntax
✅ Fast compilation
✅ Automatic dependency management
✅ Suitable for modern C++ projects
If you are tired of complex CMakeLists.txt, give Meson a try; it might just be the “build tool” you have been looking for!