spike3382
asked on
C ifdef/preprocessor question
Hi all. I'm new to programming in anything aside from UNIX, but I'm trying to pick up mac os x. I was just porting some of my old tools from UNIX to mac and ran into the following question: how do I do an #ifdef thing to tell if I'm on a mac or a UNIX box? More generally, how do you tell what (macros?) are defined on what system?
I tried
#ifdef BSD
#include whatever
#else
#include whatever
#endif
and it didn't get me too far, so I figured I'd hear from someone who knows before I learn the wrong way.
Thanks a lot!
-Matt
I tried
#ifdef BSD
#include whatever
#else
#include whatever
#endif
and it didn't get me too far, so I figured I'd hear from someone who knows before I learn the wrong way.
Thanks a lot!
-Matt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome! Thanks, Alex. This was exactly what I was looking for. Especially the gcc line near the top.
Thanks again,
Matt
Thanks again,
Matt
Predefined Macros
As specified in Apple's developer GNU C Preprocessor documentation[43], the following macros are predefined in Mac OS X:
__OBJC__
This macro is defined when you compile Objective-C .m files or Objective-C++ .mm files, or when you override the file extension with -ObjC or -ObjC++ flags.
__ASSEMBLER__
This macro is defined when compiling .s files.
__NATURAL_ALIGNMENT__
This macro is defined on systems that use natural alignment. When using natural alignment, an int is aligned on sizeof(int) boundary, a short int is aligned on sizeof(short) boundary, and so on. It's defined by default when you're compiling code for the PowerPC, SPARC, and HPPA. It's not defined when you use the -malign-mac68k compiler switch.
__STRICT_BSD__
This macro is defined if the -bsd switch was specified when GNU C was invoked.
__MACH__
This macro is defined if Mach system calls are supported.
__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.
__BIG_ENDIAN__
This macro sets the target architecture to be a most significant bit. See the Endian Issues sections for more details.
Note: To define a section of code to be compiled on Mac OS X system you should define a section using __APPLE__ with __MACH__ macros. The macro __UNIX__ is not supported in Mac OS X.