Unix flavor declaration in C / C ++

How to declare in C / C ++ that the written code should be created in HP-UX or Solaris or AIX?

+3
source share
4 answers

I found that a good way to understand this king of the issue, at least with gcc, is to have this make file:

defs:
    g++ -E -dM - < /dev/null

then:

$ make defs

should display all the definitions you have available.

So:

$ make defs | grep -i AIX
$ make defs | grep -i HP

should give you an answer. Example for Linux:

$ make defs | grep -i LINUX
#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1
#define linux 1

Once you find the definition you are looking for, you enter at the beginning of the code:

#if !(defined(HP_DEFINE) || defined(AIX_DEFINE) || defined(SOLARIS_DEFINE))
#  error This file cannot be compiled for your plateform
#endif
+15
source

What about the macro passed to the compiler?

i.e. gcc -Dmacro[=defn]

Then check the macro in the code with a simple #ifdefof #if(if you gave it a value). Perhaps a predefined macro already exists for your target platform.


[EDIT: , , -D]

-Dmacro[=defn] #define macro defn . : -Dfoo=bar #define foo bar. , , -Dfoo #define foo.

+4

, . O/S, , O/S, . , O/S, . , autoconf, autoconf, , , , . , , O/S, , , , , O/S.

. O/S , , O/S, , . Java JVM; , .

, Apache Portable Runtime (APR).

:

#ifdef HAVE_PWRITE
...code using pread() and pwrite()...
#else
...code using plain old read() and write()...
#endif

: read() write() . , , - , GCC Apache ..

+3

Perhaps a less confusing solution that some of the ones suggested is to consult the Predefined C / C ++ Compiler Macros . This site contains an extensive list of compiler macros for a large number of compiler / OS / architecture combinations.

0
source

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


All Articles