4

I am just going through some code and have come across an #ifdef that is:

#ifdef __MACH__

#define CLOCK_REALTIME 0

int clock_gettime (int /*clk_id*/, struct timespec *t) {
    struct timeval now;
    int rv = gettimeofday(&now, NULL);
    if (rv) return rv;
    t->tv_sec  = now.tv_sec;
    t->tv_nsec = now.tv_usec * 1000;
    return 0;
}
#endif

What does __MACH__ refer to here? Is it an arbitrary name for "machine" which references the current OS I am compiling on? That's really the only thing I can think of it being.

  • 1
    [apparently](https://stackoverflow.com/questions/2166483/which-macro-to-wrap-mac-os-x-specific-code-in-c-c) it refers to Mac OSX , you should find it is defined by the build system or perhaps even by the compiler – M.M Jun 23 '20 at 01:16
  • 4
    `__MACH__` is (due to the pair of leading underscores) an identifer reserved by the standard for use by the implementation. `__MACH__` in particular is one of the macros defined by implementations targeting Apple unix operating systems (OSX, iOS, and Darwin). The name is based on the "Mach Kernel", which is central to those operating systems. `__MACH__` is not defined for older (non-unix) Apple operating systems. All (supported) Apple systems may be detected using the `__APPLE__` macro, so one test for iOS/OSX/Darwin is `#if defined(__APPLE__) && defined(__MACH__)` – Peter Jun 23 '20 at 01:40
  • 1
    @Peter: https://en.wikipedia.org/wiki/Mach_(kernel) – Mark Ransom Jun 23 '20 at 01:42

1 Answers1

3

__MACH__ is a built in compiler macro that indicates if you are on Macintosh operating system. It is defined when compiling on a mac machine.