Skip to content

Cross-platform C++ library to detect the operating system and version (Windows / macOS / Linux).

License

Notifications You must be signed in to change notification settings

Evr5/OSDetect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧩 OSDetect

Build License: MIT C++17 Platform

OSDetect is a lightweight, cross-platform C++ library that detects the operating system your program is running on — including detailed versions such as Windows 10 / 11, macOS Sonama, or Linux distributions like Ubuntu, Arch, or NixOS.

🚀 Features

✅ Detects the current operating system
✅ Identifies detailed version or distribution
✅ Works on Windows, macOS, and Linux
✅ Simple, type-safe API using enum class
✅ CMake-friendly and easy to integrate

📦 Supported Platforms

OS Family Details
Windows Windows 7, 8, 10, 11 (via native RtlGetVersion)
macOS Catalina → Tahoe (kernel-based detection)
Linux Ubuntu, Debian, Fedora, Arch, openSUSE, Manjaro, CentOS, Red Hat, Alpine, NixOS, etc.

🛠 Installation

Option 1 — CMake FetchContent (recommended)

include(FetchContent)
FetchContent_Declare(
  OSDetect
  GIT_REPOSITORY https://github.com/Evr5/OSDetect.git
  GIT_TAG main
)
FetchContent_MakeAvailable(OSDetect)

target_link_libraries(your_target PRIVATE OSDetect)

Option 2 — As a submodule

git submodule add https://github.com/Evr5/OSDetect.git external/OSDetect

Then in your CMakeLists.txt:

add_subdirectory(external/OSDetect)
target_link_libraries(your_target PRIVATE OSDetect)

Option 3 — System-wide installation

git clone https://github.com/Evr5/OSDetect.git
cd OSDetect
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --target install

Then link it in your project:

find_package(OSDetect REQUIRED)
target_link_libraries(your_target PRIVATE OSDetect::OSDetect)

💡 Usage Example

#include "os_detect/OSDetect.h"
#include <iostream>

int main() {
    OSDetect::OS OS = OSDetect::getOS();
    std::cout << "Detected OS: " << OS << std::endl;

    switch (OS) {
        case OSDetect::OS::Windows:
            std::cout << "Windows Version: " << OSDetect::getWindowsVersion() << std::endl;
            break;
        case OSDetect::OS::MacOS:
            std::cout << "MacOS Version: " << OSDetect::getMacOSVersion() << std::endl;
            break;
        case OSDetect::OS::Linux:
            std::cout << "Linux Distro: " << OSDetect::getLinuxDistro() << std::endl;
            break;
        default:
            std::cout << "Unknown Version" << std::endl;
    }
    return 0;
}

Example Output:

Detected OS: Windows
Windows Version: Windows 11
Detected OS: Linux
Linux Distro: Ubuntu
Detected OS: macOS
macOS Version: macOS Sonoma (15)

📚 API Overview

Function Description
OSDetect::getOS() Returns OS::Windows, OS::MacOS, OS::Linux, or OS::Unknown.
OSDetect::getWindowsVersion() Returns a WindowsVersion enum (Windows10, Windows11, …).
OSDetect::getMacOSVersion() Returns a MacOSVersion enum (Ventura, Monterey, …).
OSDetect::getLinuxDistro() Returns a LinuxDistro enum (Ubuntu, Arch, NixOS, …).
getDetailedOS<T>() Template wrapper returning the appropriate enum.
operator<< Overloaded to print readable OS names to std::ostream.

🧱 Build the Example

Linux - macOS:

make
./osdetect_example

Windows:

make
osdetect_example.exe

📂 Directory Structure

OSDetect/
├── include/
│   └── os_detect/
│       └── OSDetect.h
├── src/
│   └── OSDetect.cpp
├── examples/
│   └── main.cpp
├── CMakeLists.txt
├── Makefile 
└── README.md

⚙️ Requirements

  • C++11 or newer
  • CMake 3.12+
  • Works with MSVC, GCC and Clang
  • Cross-platform: Windows / Linux / macOS

🧰 Contributing

Contributions and pull requests are welcome!
If your OS or Linux distribution isn’t detected correctly, open an issue or submit a PR with a sample of your /etc/os-release.

📜 License

This project is licensed under the MIT License — free for commercial and personal use.

About

Cross-platform C++ library to detect the operating system and version (Windows / macOS / Linux).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published