What C preprocessor conditional should I use for O

2019-03-10 22:50发布

What C preprocessor conditional should I use for OS X specific code? I need to include a specific library if I am compiling for OS X or a different header if I am compiling for Linux.

I know there is __APPLE__ but I don't know if that is a current conditional for OS X 10.x.

6条回答
爷、活的狠高调
2楼-- · 2019-03-10 22:56

If I remember correctly, it's __APPLE__ :)

查看更多
一夜七次
3楼-- · 2019-03-10 22:59

old style raw:

#ifdef WIN32
// windows.
#elif __APPLE__
// osx and ios.
#endif
查看更多
甜甜的少女心
4楼-- · 2019-03-10 23:08

This code example may help you -

if defined(__APPLE__)
#include "TargetConditionals.h"
  if (!defined(TARGET_OS_IPHONE) && !defined(TARGET_IPHONE_SIMULATOR))
 {
   //write your OSX specific code here
 } 
查看更多
Melony?
5楼-- · 2019-03-10 23:09

This list of operating system macros says the presence of both __APPLE__ and __MACH__ indicate OSX.

Also confirmed at line 18 of part of the source for fdisk.

查看更多
乱世女痞
6楼-- · 2019-03-10 23:17

__APPLE__ will tell you you're compiling on an Apple platform. Unless you need to support MacOS versions before OS X, that should be good enough. Alternately, you could use __APPLE__ and __MACH__ to make sure you're compiling on OS X.

查看更多
We Are One
7楼-- · 2019-03-10 23:17

This page contains a list of all OS predefined macros.

For mac OSX both the __APPLE__ && __MACH__ need to be defined.

查看更多
登录 后发表回答