Moving from xcode 3.2 to xcode 4.5.2 compilation error for 'abs' macro in stdlib.h

Discussion in 'Mac Programming' started by hrishidev, May 30, 2013.

  1. hrishidev, May 30, 2013
    Last edited by a moderator: May 31, 2013

    macrumors regular

    Joined:
    Dec 3, 2007
    #1
    I have project working fine with xcode3.2 and SDK 10.6 I checkout this project in Lion/Mountain Lion to build with Xcode 4.5.2 & SKD 10.8. But I am getting following eror

    Code:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/usr/include/stdlib.h:146:10: Expected identifier or '('
    for following code

    Code:
    define abs(a) ((a) > 0 ? (a) : -(a))
     
  2. macrumors member

    Joined:
    Dec 6, 2010
    #2
    Works fine for me in Mountain Lion using Xcode 4.6...
    Did you include the # at the beginning of the line?
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    Does your definition of an abs() macro come before or after any #include of stdlib.h?

    There is a standard C function called abs(). It's declared in stdlib.h. Read the man page for abs to see it (man abs).

    If you define your macro before the #include <stdlib.h>, then the function declaration in stdlib.h, which looks like this:
    Code:
    int  abs(int);
    
    will be macro-expanded and look like this:
    Code:
    int  ((int) > 0 ? (int) : -(int));
    
    This isn't valid C; it's nonsense.

    I recommend NOT defining a macro named abs. Name it ABS, or ABS_MACRO, or anything that isn't the same as an existing function, variable, or macro.


    Why didn't this happen before? No one knows without seeing the actual source code. Guessing would be futile.
     
  4. thread starter macrumors regular

    Joined:
    Dec 3, 2007
    #4
    Thanks guys,

    Firstly ,

    For my project , I have included ApplicationService and CoreFoundation frameworks which have include hierarchy which goes to include stdlib.h

    I have not created any macro before or after inclusion of <stdlib.h> with same name as abs and I am getting error in <stdlib.h> file and not in any of my source code
    I missed # in previous post but actual code is as follow
    Code:
    #define abs(a) ((a) > 0 ? (a) : -(a))
    


    As I said before,code works fine with xcode 3.2 and SDK 10.6.
     
  5. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    Here's a tip: Forget that it works on Xcode 3.2. There's a bug in your code, and you have to find it. This "it works in Xcode 3.2" just creates a mental block in your mind that prevents you from seeing it. The problem is not in that line in a standard header file used by millions of programmers all over the world; it is in your code.

    Since macros are involved, the obvious thing to do is Product -> Generate Output -> Preprocessed File.
     
  6. macrumors regular

    Joined:
    Aug 21, 2011
    Location:
    Stockholm Sweden
    #6
    Give us the order of include files up to the one giving the error message. Actual source code listing.

    The error message seems to indicate that there is an extra character somewhere before the line where the error occurs. It might be in your source code or in one of the included standard files. My hunch on error is that you have som odd combination of parameters which disturbs #ifdef / #ifndef conditionals.

    //Gunnar
     
  7. jaduff46, May 31, 2013
    Last edited: May 31, 2013

    macrumors member

    Joined:
    Mar 3, 2010
    #7
    Agree with Gnasher729's comment. It's your underlying code, not the version of XCode you're using.

    Abs() works on integers, fabs() works on floats, doubles. To get the floating point, you may have to include Math.h or its equivalent (#include <Math.h>).

    Why reinvent the wheel. Use what's available and already debugged.
     
  8. macrumors 6502a

    Detektiv-Pinky

    Joined:
    Feb 25, 2006
    Location:
    Berlin, Germany
    #8
    Get rid of
    Code:
    #define abs(a) ((a) > 0 ? (a) : -(a))
    You are redefining an already existing function from stdlib.
    The simplest way to avoid this sort of problems, is by naming your own functions and macros so that they NEVER clash with existing library functions. See the recommendations from user chown33.
     

Share This Page