How to Check the fmtlib Version in C++: Compile-Time and Runtime Methods

The fmtlib version is defined as the macro FMT_VERSION in include/fmt/core.h, with the current release encoding version 12.2.1 as the integer 120201 using the formula major×10000 + minor×100 + patch.

The {fmt} library is a fast, open-source C++ formatting library that provides a safe and extensible alternative to printf and iostream. Accessing the library version at compile time is essential for feature detection, conditional compilation, and diagnostic logging in cross-platform applications.

Where the fmtlib Version is Defined

The FMT_VERSION Macro in core.h

The authoritative source for the fmtlib version is located in include/fmt/core.h at lines 23-25. The library exposes a single integer macro that encodes the semantic version components:

#define FMT_VERSION 120201

This definition is part of the core API introduced in include/fmt/core.h, making it available to any translation unit that includes the header. The value is an integer literal rather than a string, enabling direct comparison operations in preprocessor conditionals.

How to Decode the fmtlib Version Number

The fmtlib project uses a compact integer encoding scheme to represent semantic versioning. The formula is:

major × 10000 + minor × 100 + patch

Breaking down the current value 120201:

  • Major: 120201 ÷ 10000 = 12
  • Minor: (120201 % 10000) ÷ 100 = 2
  • Patch: 120201 % 100 = 1

Thus, FMT_VERSION 120201 represents fmtlib 12.2.1.

Practical Code Examples

You can extract and display the version components using standard integer arithmetic:

#include <fmt/core.h>
#include <iostream>

int main() {
    int major = FMT_VERSION / 10000;
    int minor = (FMT_VERSION % 10000) / 100;
    int patch = FMT_VERSION % 100;
    std::cout << "fmt version: " << major << '.' << minor << '.' << patch << '\n';
}

This approach requires no runtime dependencies beyond the header itself, making it suitable for initialization logging and diagnostic reporting.

Compile-Time Feature Gating

Use the macro in preprocessor directives to conditionally enable features based on the library version:

#include <fmt/core.h>

#if FMT_VERSION >= 120200   // Require at least fmt 12.2.0
    #define HAVE_NEW_API
#endif

#ifdef HAVE_NEW_API
static_assert(true, "New API is available");
#endif

This pattern is critical for maintaining compatibility across different fmtlib versions in header-only or statically linked projects.

Embed Version in Formatted Output

Leverage the library's own formatting capabilities to print the version string:

#include <fmt/core.h>

int main() {
    fmt::print("Running fmt {}.{}.{}\n",
               FMT_VERSION / 10000,
               (FMT_VERSION % 10000) / 100,
               FMT_VERSION % 100);
}

This demonstrates how to use the fmt::print function while simultaneously reporting the library version that provides it.

Build System Integration

The version defined in include/fmt/core.h is propagated to the build system through CMakeLists.txt. The CMake configuration reads the header definition to ensure consistency between the compiled library and the headers, generating appropriate FMT_VERSION definitions for consumers. For historical context and migration notes, consult ChangeLog.md, which records the version history and breaking changes for each release.

Summary

  • The fmtlib version is defined as FMT_VERSION in include/fmt/core.h at lines 23-25.
  • The current version 12.2.1 is encoded as the integer 120201.
  • The encoding formula is major×10000 + minor×100 + patch, enabling simple preprocessor arithmetic.
  • You can check the version at compile time using #if FMT_VERSION >= XXXXXX for conditional compilation.
  • Runtime version extraction requires dividing the macro by 10000 and 100 to isolate major, minor, and patch components.

Frequently Asked Questions

How do I find my installed fmtlib version?

Include fmt/core.h and check the value of FMT_VERSION. Divide the integer by 10000 to get the major version, or use the modulo operations described above to extract all three components. If you installed fmtlib via a package manager, you can also check ChangeLog.md in the installation directory to see the release notes for your specific version.

What version format does fmtlib use?

fmtlib uses a three-component semantic versioning scheme (major.minor.patch) encoded into a single integer. The encoding places the major version in the ten-thousands place, the minor version in the hundreds place, and the patch version in the ones place. This compact representation allows for efficient compile-time comparisons using standard C preprocessor operators.

Can I check the fmtlib version at compile time?

Yes. Because FMT_VERSION is a preprocessor macro defined in include/fmt/core.h, you can use it in #if directives to conditionally compile code based on the available library version. This is the recommended approach for writing portable code that supports multiple fmtlib releases, particularly when using features introduced in specific versions.

Where is the fmtlib version history documented?

The complete version history is recorded in ChangeLog.md at the repository root. This file contains detailed release notes, breaking changes, and new features introduced in each version. The README.md file also provides an overview of the library's goals and current capabilities, though specific version details reside in the changelog and the FMT_VERSION macro definition.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →