DEV Community

Maysara
Maysara

Posted on

C++ (Cross-Platform) Predefined Library

C++ Predefined Macros (Cross-Platform)

As we know there is no standard way in compilers to standardize macros with compilers and different operating systems

So I created this library that contains standardized macros and more.

Github Repository


Contents :

  • Architecture

    This section contains some macros that can detect more than 20 architecture.

  • Operating System

    This section contains some macros that can detect more than 66 operating system.

  • Compiler

    This section contains some macros that can detect more than 75 compiler and its version.

  • Library

    This section contains some macros that can detect more than 11 library and its version.

  • Standard

    This section contains the standard macros more than 35 macro.

  • Has Macros

    This section contains the __has macros more than 4 macros.

  • Attributes

    This section contains some standard and common attributes more than 13 attribute.

  • Controllers

    This section contains some macros that can manage some things in C/C++ more than 7 controllers.

  • Helpers Macros

    This section contains some macros that may be useful while writting this library.


Quick Examples

  /*                  Print architecture name                   */
  cout << X_ARCH                    << endl; // πŸ‘‰ "x86_64"

  /*       Check for specific architecture using ID/Macro       */
  #ifdef X_ARCH_<ID>
  ...
Enter fullscreen mode Exit fullscreen mode
  /*                 Print operating system name                */
  cout << X_OS                      << endl; // πŸ‘‰ "GNU/Linux"

  /*            Check for specific OS using ID/Macro            */
  #ifdef X_OS_<ID>
  ...
Enter fullscreen mode Exit fullscreen mode
  /*                    Print compiler info                     */
  cout << X_COMP                << endl; // πŸ‘‰ "GCC"
  cout << X_COMP_MAJOR          << endl; // πŸ‘‰ 12
  cout << X_COMP_MINOR          << endl; // πŸ‘‰ 1
  cout << X_COMP_PATCH          << endl; // πŸ‘‰ 0
  cout << X_COMP_VERSION        << endl; // πŸ‘‰ 201392128
  cout << X_COMP_ORG_VERSION    << endl; // πŸ‘‰ 12

  /*         Check for specific compiler using ID/Macro         */
  #ifdef X_COMP_<ID>
  ...
Enter fullscreen mode Exit fullscreen mode
  /*                       Print lib info                       */
  cout << X_LIB                     << endl; // πŸ‘‰ "GNU glibc"
  cout << X_LIB_MAJOR               << endl; // πŸ‘‰ 2
  cout << X_LIB_MINOR               << endl; // πŸ‘‰ 35
  cout << X_LIB_PATCH               << endl; // πŸ‘‰ 0
  cout << X_LIB_VERSION             << endl; // πŸ‘‰ 35848192
  cout << X_LIB_ORG_VERSION         << endl; // πŸ‘‰ 2

  /*           Check for specific lib using ID/Macro            */
  #ifdef X_LIB_<ID>
  ...
Enter fullscreen mode Exit fullscreen mode
  /*                       Print some info                      */
  cout << X_STD_CPP                 << endl; // πŸ‘‰ 20
  cout << X_STD_FILE                << endl; // πŸ‘‰ "path/main.cpp"
  cout << X_STD_FUNC                << endl; // πŸ‘‰ "main()"
  cout << X_STD_LINE                << endl; // πŸ‘‰ 4
  cout << X_STD_TIME                << endl; // πŸ‘‰ "10:19:16"
  cout << X_STD_DATE                << endl; // πŸ‘‰ "Oct 30 2022"

  /*           Check for specific Version                       */
  #if X_STD_CPP >= 20 // you can use (X_STD_C) for c language
  ...
Enter fullscreen mode Exit fullscreen mode
  /*                    Check for Attribute                     */
  #if X_HAS_ATTR(noreturn)
  ...

  /*                    Check for Feature                       */
  #if X_HAS_FEATURE(cxx_rvalue_references)
  ...

  /*                    Check for Include                       */
  #if X_HAS_INCLUDE("iostream")
  ...

  /*                    Check for Builtin                       */
  #if X_HAS_BUILTIN(__builtin_clz)
  ...
Enter fullscreen mode Exit fullscreen mode
  /*                    Usage of Attributes                     */
  X_ATTR_DEPRECATED("old version")
  X_ATTR_INLINE
  X_ATTR_HIDDEN
  void func(X_ATTR_MAYBE_UNUSED int i = 0)
  ...
Enter fullscreen mode Exit fullscreen mode
  /*                   Manage Exceptions                        */
  X_CONSTEXPR void func() X_NOEXCEPT
  // IF C++ && X_EXCEPTIONS  => constexpr void func() noexcept
  // ELSE                    => void func()
  ...

  /*                    Throw An Exception                      */
  X_THROW(exception);
  // IF C++ && X_EXCEPTIONS  => throw an exception
  // ELSE IF C++             => assert an error with exception message
  // ELSE                    => assert an error without        message
Enter fullscreen mode Exit fullscreen mode
  /*        Version Normalization (major, minor, patch)         */
  #define version X_VER(9,3,7)
Enter fullscreen mode Exit fullscreen mode

Tests

This library has been tested on all of the following:

Operating System

  • Linux Ubuntu 22.04.1 LTS
  • Windows Windows 10 pro

Compiler

  • GCC 12.1
  • Clang 15.0.0
  • MSVC 19.0

References

Top comments (0)