Which MACro to Wrap MAC Os X Specific Code in C/C++

Which macro to wrap Mac OS X specific code in C/C++

It all depends.

Each macro specifies something different in meaning.

See: https://developer.apple.com/library/mac/documentation/Porting/Conceptual/PortingUnix/compiling/compiling.html#//apple_ref/doc/uid/TP40002850-SW13

__APPLE__

This macro is defined in any Apple computer.

__APPLE_CC__

This macro is set to an integer that represents the version number of
the compiler. This lets you distinguish, for example, between compilers
based on the same version of GCC, but with different bug fixes or features.
Larger values denote later compilers.

__OSX__

Presumably the OS is a particular variant of OS X

So given the above definitions I would use __APPLE__ to distinguish apple specific code.

What macro to use to identify Mavericks OSX 10.9 in c/c++ code?


#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_9
<Put your Mavericks specific code here..>
#else
<Put all other code here..>
#endif

This works for other versions as well. For other macros relating to OSX versions, see AvailiabilityMacros.h.

C macro wrapping

This is a hack to trick the C preprocessor to pre-expand the parameters before they're concatenated. This kind of decoupling is required in certain some situations, for example if __COMB is to be used in a preprocessor macro parameter stringification.

What does __MACH__ mean for an #ifdef?

__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.

Preprocessor macro for OS X targets?

That is because TARGET_OS_MAC is defined when building for iOS as well.

See http://sealiesoftware.com/blog/archive/2010/8/16/TargetConditionalsh.html on that.

I would try and build my own target specific define via build-settings on the target.

Writing cross-platform C++ Code (Windows, Linux and Mac OSX)

I'll address this specific function:

bool probe() {
#ifdef TARGET_OS_MAC
return probe_macosx();
#elif defined __linux__
return probe_linux();
#elif defined _WIN32 || defined _WIN64
return probe_win();
#else
#error "unknown platform"
#endif
}

Writing it this way, as a chain of if-elif-else, eliminates the error because it's impossible to compile without either a valid return statement or hitting the #error.

(I believe WIN32 is defined for both 32- and 64-bit Windows, but I couldn't tell you definitively without looking it up. That would simplify the code.)


Unfortunately, you can't use #ifdef _WIN32 || _WIN64: see http://codepad.org/3PArXCxo for a sample error message. You can use the special preprocessing-only defined operator, as I did above.


Regarding splitting up platforms according to functions or entire files (as suggested), you may or may not want to do that. It's going to depend on details of your code, such as how much is shared between platforms and what you (or your team) find best to keep functionality in sync, among other issues.

Furthermore, you should handle platform selection in your build system, but this doesn't mean you can't use the preprocessor: use macros conditionally defined (by the makefile or build system) for each platform. In fact, this is the often the most practical solution with templates and inline functions, which makes it more flexible than trying to eliminate the preprocessor. It combines well with the whole-file approach, so you still use that where appropriate.

You might want to have a single config header which translates all the various compiler- and platform-specific macros into well-known and understood macros that you control. Or you could add -DBEAKS_PLAT_LINUX to your compiler command line—through your build system—to define that macro (remember to use a prefix for macro names).



Related Topics



Leave a reply



Submit