Platform-Specific Macro Documentation on Linux / POSIX

When compiling a C / C ++ program under Windows using Visual Studio (or a compiler that tries to be compatible) there is a predefined macro _WIN32 (Source: http://msdn.microsoft.com/en-us/library/b0084kay.aspx ), which you can use for # -dedef-s for the platform.

I am looking for an analogue under Linux: a macro that tells me that I am compiling for Linux / OS, which claims to be (more or less) POSIX compatible.

So, I looked through the gcc documentation and found this: http://gcc.gnu.org/onlinedocs/cpp/System_002dspecific-Predefined-Macros.html

For my program, the following macros (gcc 4.4.5 - Ubuntu 10.10) looked promising (I hope that I have not lost an important macro):

#define __USE_BSD 1 #define __unix__ 1 #define __linux 1 #define __unix 1 #define __linux__ 1 #define _POSIX_SOURCE 1 #define __STDC_HOSTED__ 1 #define __STDC_IEC_559__ 1 #define __gnu_linux__ 1 #define __USE_SVID 1 #define __USE_XOPEN2K 1 #define __USE_POSIX199506 1 #define _G_USING_THUNKS 1 #define __USE_XOPEN2K8 1 #define _BSD_SOURCE 1 #define unix 1 #define linux 1 #define __USE_POSIX 1 #define __USE_POSIX199309 1 #define __SSP__ 1 #define _SVID_SOURCE 1 #define _G_HAVE_SYS_CDEFS 1 #define __USE_POSIX_IMPLICITLY 1 

Where can I find detailed documentation on them - regarding the Windows macros mentioned above?

In addition, I would be interested in macros that are usually defined for other POSIX compatible operating systems: * BSD, etc.

+4
source share
3 answers

I once compiled identification macros to use pkgsrc, a cross-platform package system: http://www.netbsd.org/docs/pkgsrc/fixes.html#fixes.build.cpp

I did not find an authoritative source then.

+3
source

The glibc manual lists some of these macros, including _POSIX_SOURCE . However, they work differently than you might expect: the programmer defines these macros (possibly in the script assembly), and the C library headers check it to enable or disable certain functions.

+6
source

There are currently two games in the city: Windows and POSIX. Therefore, you can probably get what you want with #ifndef _WIN32 . The differences between the surviving Unix variants are best made using autoconf-style test functions, for example. #ifdef HAVE_SYS_WHATEVER_H ; but don’t worry with any of them until you know that you need it. Many of the stock tests in the autoconf manual are not currently needed.

(VMS and what IBM calls its non-Unixy-mainframe OS these days may still be considered more games in the city, but I would not try to be portable to any of them until someone asks for this.)

+2
source

Source: https://habr.com/ru/post/1333951/


All Articles