PDA

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




hrishidev
May 30, 2013, 03:24 PM
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/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/usr/include/stdlib.h:146:10: Expected identifier or '('

for following code

define abs(a) ((a) > 0 ? (a) : -(a))



truehybridx
May 30, 2013, 05:00 PM
Works fine for me in Mountain Lion using Xcode 4.6...
Did you include the # at the beginning of the line?

chown33
May 30, 2013, 06:58 PM
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.

hrishidev
May 31, 2013, 02:29 AM
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.

gnasher729
May 31, 2013, 05:12 AM
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.

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.

ghellquist
May 31, 2013, 06:46 AM
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

jaduff46
May 31, 2013, 08:58 AM
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.

Detektiv-Pinky
Jun 2, 2013, 10:36 AM
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.

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.