C++ (Cross-Platform) Predefined Library
Tags: cpp
Published: 2022-11-05 14:02:59

# 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](https://github.com/xeerx/cpp-predefined)

---

**Contents :**

- **[Architecture](#architecture)** <br>
  This section contains some macros that can detect **more than 20 architecture**.

- **[Operating System](#operating-system)** <br>
  This section contains some macros that can detect **more than 66 operating system**.

- **[Compiler](#compiler)** <br>
  This section contains some macros that can detect **more than 75 compiler and its version**.

- **[Library](#library)** <br>
  This section contains some macros that can detect **more than 11 library and its version**.

- **[Standard](#standard)** <br>
  This section contains the standard macros **more than 35 macro**.

- **[Has Macros](#has-macros)** <br>
  This section contains the `__has` macros **more than 4 macros**.

- **[Attributes](#attributes)** <br>
  This section contains some standard and common attributes **more than 13 attribute**.

- **[Controllers](#controllers)** <br>
  This section contains some macros that can manage some things in C/C++ **more than 7 controllers**.

- **[Helpers Macros](#helpers-macros)** <br>
  This section contains some macros that may be useful while writting this library.

---

## Quick Examples

- [Architecture](/docs/architectures.md)

  ```cpp
  /*                  Print architecture name                   */
  cout << X_ARCH                    << endl; // 👉 "x86_64"

  /*       Check for specific architecture using ID/Macro       */
  #ifdef X_ARCH_<ID>
  ...
  ```

- [Operating System](/docs/operating-system.md)

  ```cpp
  /*                 Print operating system name                */
  cout << X_OS                      << endl; // 👉 "GNU/Linux"

  /*            Check for specific OS using ID/Macro            */
  #ifdef X_OS_<ID>
  ...
  ```

- [Compiler](/docs/compiler.md)

  ```cpp
  /*                    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>
  ...
  ```

- [Library](/docs/library.md)

  ```cpp
  /*                       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>
  ...
  ```

- [Standard](/docs/standards.md)

  ```cpp
  /*                       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
  ...
  ```

- [Has-Macros](/docs/has-macros.md)

  ```cpp
  /*                    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)
  ...
  ```

- [Attributes](/docs/attributes.md)

  ```cpp
  /*                    Usage of Attributes                     */
  X_ATTR_DEPRECATED("old version")
  X_ATTR_INLINE
  X_ATTR_HIDDEN
  void func(X_ATTR_MAYBE_UNUSED int i = 0)
  ...
  ```

- [Controllers](/docs/controllers.md)

  ```cpp
  /*                   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
  ```

- [Helpers Macros](/docs/helpers-macros.md)

  ```cpp
  /*        Version Normalization (major, minor, patch)         */
  #define version X_VER(9,3,7)
  ```

---

### 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

- https://sourceforge.net/p/predef/wiki/Home/
- https://gcc.gnu.org/onlinedocs/gcc/
- https://learn.microsoft.com/en-us/cpp/preprocessor/