This content originally appeared on DEV Community ๐ฉโ๐ป๐จโ๐ป and was authored by Maysara
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.
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>
...
/* Print operating system name */
cout << X_OS << endl; // ๐ "GNU/Linux"
/* Check for specific OS using ID/Macro */
#ifdef X_OS_<ID>
...
/* 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>
...
/* 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>
...
/* 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
...
/* 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)
...
/* Usage of Attributes */
X_ATTR_DEPRECATED("old version")
X_ATTR_INLINE
X_ATTR_HIDDEN
void func(X_ATTR_MAYBE_UNUSED int i = 0)
...
/* 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
/* 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/
This content originally appeared on DEV Community ๐ฉโ๐ป๐จโ๐ป and was authored by Maysara
Maysara | Sciencx (2022-11-05T14:02:59+00:00) C++ (Cross-Platform) Predefined Library. Retrieved from https://www.scien.cx/2022/11/05/c-cross-platform-predefined-library/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.