Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hrishidev

macrumors regular
Original poster
Dec 3, 2007
107
4
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))
 
Last edited by a moderator:

truehybridx

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

chown33

Moderator
Staff member
Aug 9, 2009
10,679
8,303
A sea of green
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.
 

hrishidev

macrumors regular
Original poster
Dec 3, 2007
107
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.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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.

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

macrumors regular
Aug 21, 2011
146
5
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.

//Gunnar
 

jaduff46

macrumors 6502
Mar 3, 2010
328
187
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.
 
Last edited:

Detektiv-Pinky

macrumors 6502a
Feb 25, 2006
848
192
Berlin, Germany
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.