Differences between #includes in Linux and OS X

Discussion in 'Mac Programming' started by foidulus, Feb 9, 2011.

  1. foidulus macrumors 6502a

    Joined:
    Jan 15, 2007
    #1
    I am currently working on a research tool at school and am wondering why in a couple of cases the C code compiles fine in Linux, but in OS X a couple of separate includes(such as sys.h, types.h etc) are necessary. After the includes the software compiles and runs fine, but I'm a bit of a C newb, so I'm not quite sure why they are necessary.

    Is there actually a difference between how gcc locates header files on OS X vs. Linux? Is there any way I can get OS X to behave more like Linux?

    I've just been putting in the extra includes, shouldn't hurt anything.
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    You should probably look into conditional includes, for example.

    Code:
    #ifdef __APPLE__
          #define <whatever.h>
    
     
  3. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    These are header files outside of the C language specification. They are platform header files defined by various other standards such as POSIX and the Single Unix Standard. What I think you're encountering are issues with platform differences. You're actually porting Linux code to Mac OS X. Luckily though Mac OS X and Linux are close enough that all you need to do include extra header files.

    If you want to the code to compile under both "out of the box", you could surround the includes with #ifdef's. Or investigate autoconf, but that's probably overkill.
     
  4. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #4
    Some older versions of gcc or specific forked builds automatically define a set of headers but I though that was removed after 3.x. Might want to use the same compiler on both machines and see how that performs.
     

Share This Page