Fmtlib: The Modern C++ Formatting Library
fmtlib/fmt is a modern, open-source formatting library for C++. Think of it as a much better alternative to the old-school <iostream> and printf. For a software engineer, its utility comes down to three key areas
unicode support, performance, and cross-platform compatibility.
Unicode Support
In today's globalized world, handling a variety of characters from different languages is crucial. Standard C++ streams can be clunky with Unicode. fmt handles Unicode effortlessly, making your code more robust for internationalization and localization (i18n and l10n). You don't have to worry about character encoding issues breaking your output.
Performance
One of the biggest advantages of fmt is its speed. It's significantly faster than C++ streams (std::cout, std::stringstream). For applications where performance is critical, like game development, high-frequency trading, or embedded systems, fmt can make a real difference. It achieves this by avoiding complex object hierarchies and virtual function calls, and by being highly optimized at compile time.
Cross-Platform
fmt is a header-only library (in most cases), which makes it incredibly easy to integrate into your projects. It works seamlessly across different operating systems (Windows, macOS, Linux) and compilers (GCC, Clang, MSVC), ensuring your code is portable without a lot of headaches.
In essence, fmt allows you to write clean, fast, and safe formatting code. It provides a C-style printf-like syntax but with the type safety and extensibility of C++. No more printf format string vulnerabilities or std::cout's verbose syntax.
Getting started with fmt is a breeze. The most common and recommended way is to use a package manager like Conan or vcpkg.
This is a modern, simple way to include fmt directly into your CMake project without an external package manager. It's great for projects where you want a self-contained build.
Add this to your CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(MyProject LANGUAGES CXX)
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 10.1.1 # Use the latest version or a specific tag
)
FetchContent_MakeAvailable(fmt)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE fmt::fmt)
Now, your project will automatically download and build fmt for you.
If you're already using a package manager, this is a straightforward option.
Install vcpkg
Follow the instructions on the official vcpkg GitHub page.
Install fmt
Open your terminal and run
vcpkg install fmt
Integrate with CMake
In your CMakeLists.txt, add this line at the top
find_package(fmt REQUIRED)
And link the library
target_link_libraries(my_app PRIVATE fmt::fmt)
Let's see fmt in action. The syntax is a lot like printf, but with more power and safety.
This is the classic "Hello, World!" of fmt.
#include <fmt/core.h>
int main() {
fmt::print("Hello, {}!\n", "world");
return 0;
}
Output
Hello, world!
Notice the {} placeholder. It's a clean way to insert values.
You can pass as many arguments as you need, and even control their order.
#include <fmt/core.h>
int main() {
fmt::print("The answer is {} and the meaning is {}.\n", 42, "everything");
// Positional arguments
fmt::print("I'm {0}, and I like to {1} {0} and {2}!\n", "John", "eat", "play");
return 0;
}
Output
The answer is 42 and the meaning is everything.
I'm John, and I like to eat John and play!
fmt gives you great control over number precision, padding, and alignment. This is super useful for generating reports or formatted output.
#include <fmt/core.h>
int main() {
// Left-align text
fmt::print("{:<10}\n", "left");
// Right-align text
fmt::print("{:>10}\n", "right");
// Fixed-point number with 2 decimal places
fmt::print("The value is {:.2f}\n", 3.14159);
// Print in hexadecimal
fmt::print("Decimal: {}, Hex: {:#x}\n", 255, 255);
return 0;
}
Output
left
right
The value is 3.14
Decimal: 255, Hex: 0xff