Solved

C ifdef/preprocessor question

Posted on 2004-09-10
3
1,399 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:spike3382
3 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12031494
Aside from the common GCC predefined, GCC on Mac OS X supplies the following additional predefined macros:

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.


0
 
LVL 4

Accepted Solution

by:
Alex Curylo earned 105 total points
ID: 12046419
Here's a piece taken from my platform compatability include header for a current project that uses compiler intrinsics to distinguish between GCC on OS X/Linux/OpenBSD/generic UNIX; Metrowerks CodeWarrior on OS X; and Microsoft Visual C++ on CE and Windows; and sets a byte order define too. This should be a good start for you.

/*
 *      gcc based compiler
 *
 * To see a full list of compiler defined symbols, use
 * gcc -dM -E - < /dev/null
 */
#if defined(__GNUC__)
   
   /*
    *      used on Mac OS X
    */
   #if defined(__APPLE_CPP__) || defined(__APPLE_CC__) || defined(__MACOS_CLASSIC__)

      #define VS_TARGET_OS_MAC        1
      #define VS_TARGET_RT_MAC_MACHO  1
     
   /*
    *      used on Linux
    */
   #elif defined(__linux__) || defined(linux) || defined(__linux)

      #define VS_TARGET_OS_LINUX    1

   /*
    *      used on OpenBSD
    */
   #elif defined(__OpenBSD__)

      #define VS_TARGET_OS_OPENBSD    1

   /*
    *      used on some other UNIX flavour -- OpenBSD setting should work for that too we expect
    */
   #elif defined(__unix__) || defined(unix) || defined(__unix)

      #define VS_TARGET_OS_OPENBSD    1

   /*
    *      used on unknown OS
    */
   #else
     
      /*
      NOTE:   If your compiler errors out here then support for your platform
      has not yet been added to VsPlatformConfig.h.  

      VsPlatformConfig.h is designed to be plug-and-play.  It auto detects
      which compiler is being run and configures the VS_TARGET_ conditionals
      appropriately.  

      The short term work around is to set the VS_TARGET_CPU_ and VS_TARGET_OS_
      on the command line to the compiler (e.g. d VS_TARGET_CPU_MIPS -d VS_TARGET_OS_LINUX)

      The long term solution is to add a new case to this file which
      auto detects your compiler and sets up the VS_TARGET_ conditionals.
      */
      #error VsPlatformConfig.h: unknown GNU C platform (see comment above)
     
   #endif /* GCC/OS defines */

   /*
    *      used on PowerPC CPU
    */
   #if defined(__ppc__) || defined(powerpc) || defined(ppc) || defined(_ARCH_PPC)
      #define VS_TARGET_CPU_PPC          1
      #define VS_TARGET_RT_BIG_ENDIAN    1

   /*
    *      used on x86 CPU
    */
   #elif defined(__i386__) || defined(i386) || defined(__i386) || defined(intel)
      #define VS_TARGET_CPU_X86          1
      #define VS_TARGET_RT_LITTLE_ENDIAN 1

   /*
    *      used on unknown CPU
    */
   #else
     
      /*
      NOTE:   If your compiler errors out here then support for your CPU
      has not yet been added to VsPlatformConfig.h.  

      VsPlatformConfig.h is designed to be plug-and-play.  It auto detects
      which compiler is being run and configures the VS_TARGET_ conditionals
      appropriately.  

      The short term work around is to set the VS_TARGET_CPU_ and VS_TARGET_OS_
      on the command line to the compiler (e.g. d VS_TARGET_CPU_MIPS -d VS_TARGET_OS_LINUX)

      The long term solution is to add a new case to this file which
      auto detects your compiler and sets up the VS_TARGET_ conditionals.
      */
      #error VsPlatformConfig.h: unknown GNU C processor (see comment above)
     
   #endif /* GCC/CPU defines */

/*
 *   CodeWarrior compiler from Metrowerks/Motorola -- Mac OS X only
 */
#elif defined(__MWERKS__)

   #define VS_TARGET_OS_MAC           1
   #define VS_TARGET_RT_MAC_MACHO     1
      
      #if defined(__POWERPC__)
            #define VS_TARGET_CPU_PPC          1
            #define VS_TARGET_RT_BIG_ENDIAN    1
      #elif defined(__INTEL__)
            #define VS_TARGET_CPU_X86          1
            #define VS_TARGET_RT_LITTLE_ENDIAN 1
      #else
            #error VsPlatformConfig.h: unknown Metrowerks CPU type
      #endif

      #ifdef __MACH__
            #define VS_TARGET_RT_MAC_MACHO     1
      #else
            #define VS_TARGET_RT_MAC_CFM       1
      #endif

/*
 *   Visual Studio C/C++ from Microsoft, Inc. -- Win32 and WinCE only
 */
#elif defined(_MSC_VER)

    #define VS_TARGET_CPU_X86              1
    #define VS_TARGET_RT_LITTLE_ENDIAN     1
   
    #ifdef UNDER_CE
          #define VS_TARGET_OS_WINCE         1
    #else
          #define VS_TARGET_OS_WIN32         1
    #endif UNDER_CE

/*
 *   unknown compiler
 */
#else

   /*
   NOTE:   If your compiler errors out here then support for your compiler
   has not yet been added to VsPlatformConfig.h.  

   VsPlatformConfig.h is designed to be plug-and-play.  It auto detects
   which compiler is being run and configures the VS_TARGET_ conditionals
   appropriately.  

   The short term work around is to set the VS_TARGET_CPU_ and VS_TARGET_OS_
   on the command line to the compiler (e.g. d VS_TARGET_CPU_MIPS -d VS_TARGET_OS_LINUX)

   The long term solution is to add a new case to this file which
   auto detects your compiler and sets up the VS_TARGET_ conditionals.
   */
   #error VsPlatformConfig.h: unknown compiler (see comment above)
      
#endif /* compiler defines */
0
 
LVL 1

Author Comment

by:spike3382
ID: 12049622
Awesome!  Thanks, Alex.  This was exactly what I was looking for.  Especially the gcc line near the top.

Thanks again,
Matt
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question