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

    hrishidev macrumors regular

    Dec 3, 2007
    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

    /Applications/ Expected identifier or '('
    for following code

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

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

    Aug 9, 2009
    Sailing beyond the sunset
    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:
    int  abs(int);
    will be macro-expanded and look like this:
    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. hrishidev thread starter macrumors regular

    Dec 3, 2007
    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
    #define abs(a) ((a) > 0 ? (a) : -(a))

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


    Nov 25, 2005
    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. ghellquist macrumors regular

    Aug 21, 2011
    Stockholm Sweden
    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.

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

    jaduff46 macrumors 6502


    Mar 3, 2010
    Second star on the right....
    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. Detektiv-Pinky macrumors 6502a


    Feb 25, 2006
    Berlin, Germany
    Get rid of
    #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