Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Snow Leopard breaks GCC?

I'm attempting to learn Objective C, and I have a very simple script that compiles just fine on 10.5.8, but gives me the following error on Snow Leopard:


fraction.m: In function 'main':
fraction.m:6: warning: 'Fraction' may not respond to '+alloc'
fraction.m:6: warning: (Messages without a matching method signature
fraction.m:6: warning: will be assumed to return 'id' and accept
fraction.m:6: warning: '...' as arguments.)
fraction.m:7: warning: 'Fraction' may not respond to '-init'
fraction.m:18: warning: 'Fraction' may not respond to '-free'


I have installed the most recent version of XCode and the iPhone SDK.

Can anyone shed some light on this? My program can be found below. Thank you!


#import <stdio.h>
#import <objc/Object.h>
/* Interface */
@interface Fraction: Object
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
/* Implementation */
@implementation Fraction;
-(void) print
{
printf ("%i,%i", numerator, denominator);
}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(int) numerator {
return numerator;
}
-(int) denominator {
return denominator;
}
@end
#include "fraction.h"
int main (int argc, char *argv[])
{

// Fraction *myFraction = [[Fraction alloc] init];

Fraction *myFraction;
myFraction = [Fraction alloc];
myFraction = [myFraction init];

// Set fraction to 1/3

[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// Display the fraction

printf ("The value of the fraction is %i/%i", [myFraction numerator], [myFraction denominator]);

[myFraction free];

return 0;

}

iMac, Mac OS X (10.6)

Posted on Sep 2, 2009 7:58 AM

Reply
19 replies

Sep 2, 2009 9:12 AM in response to Cohi

Thank you for your reply...

How do I move forward now? If I try to compile my program on Snow Leopard, I get the warnings, and I do get an output file. But the compiled program also does not run. It gives me this error:


objc[12221]: Fraction: Does not recognize selector forward::
Illegal instruction


The same compiled program runs just fine on 10.5.8.

Sep 2, 2009 9:22 AM in response to Cohi

I honestly don't know. I tried changing this:


#import <objc/Object.h>


to this:


#import <foundation/NSObject.h>


And now it's really blowing up:


fraction.h:7: error: cannot find interface declaration for 'Object', superclass of 'Fraction'
fraction.m: In function 'main':
fraction.m:6: warning: 'Fraction' may not respond to '+alloc'
fraction.m:6: warning: (Messages without a matching method signature
fraction.m:6: warning: will be assumed to return 'id' and accept
fraction.m:6: warning: '...' as arguments.)
fraction.m:7: warning: 'Fraction' may not respond to '-init'
fraction.m:18: warning: 'Fraction' may not respond to '-free'


I'm more than happy to learn all this stuff, but can someone with some experience in Objective C set me on the right path here?

Sep 2, 2009 9:56 AM in response to Cohi

Here is my code:


#import <stdio.h>
#import <foundation/NSObject.h>
/* Interface */
@interface Fraction: NSObject
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
/* Implementation */
@implementation Fraction;
-(void) print
{
printf ("%i,%i", numerator, denominator);
}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(int) numerator {
return numerator;
}
-(int) denominator {
return denominator;
}
@end
int main (int argc, char *argv[])
{
Fraction *myFraction;
myFraction = [Fraction alloc];
myFraction = [myFraction init];

// Set fraction to 1/3

[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// Display the fraction

printf ("The value of the fraction is %i/%i ", [myFraction numerator], [myFraction denominator]);

[myFraction free];

return 0;
}


Here is the response I receive from gcc:


gcc test.m -o output -l objc
test.m: In function 'main':
test.m:62: warning: 'Fraction' may not respond to '-free'
test.m:62: warning: (Messages without a matching method signature
test.m:62: warning: will be assumed to return 'id' and accept
test.m:62: warning: '...' as arguments.)
Undefined symbols:
"_OBJC_CLASS_$_NSObject", referenced from:
_OBJC_CLASS_$_Fraction in ccN2eibr.o
"_OBJC_METACLASS_$_NSObject", referenced from:
_OBJC_METACLASS_$_Fraction in ccN2eibr.o
_OBJC_METACLASS_$_Fraction in ccN2eibr.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Sep 2, 2009 3:26 PM in response to tkambler

tkambler wrote:
I am reading "Programming in Objective C" by Stephen Kochan:
The book states that the examples apply to OS X. I guess that's not true?


I can't comment on that. You can compile that code on OS X, if you have all the correct command line parameters. But no one does objective-c outside of Xcode, from the command line.

Sep 2, 2009 11:08 PM in response to etresoft

You just need to link against the Foundation framework.


colin@weasel:/tmp> cat fraction.m
#import <stdio.h>
#import <Foundation/NSObject.h>

@interface Fraction: NSObject
{
int numerator;
int denominator;
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;

@end

@implementation Fraction;

-(void) print
{
printf ("%i,%i", numerator, denominator);
}

-(void) setNumerator: (int) n {
numerator = n;
}

-(void) setDenominator: (int) d {
denominator = d;
}

-(int) numerator {
return numerator;
}

-(int) denominator {
return denominator;
}

@end

int main (int argc, char *argv[])
{
Fraction *myFraction;
myFraction = [Fraction alloc];
myFraction = [myFraction init];

// Set fraction to 1/3

[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// Display the fraction

printf ("The value of the fraction is %i/%i\n", [myFraction numerator], [myFraction denominator]);

[myFraction free];

return 0;
}

colin@weasel:/tmp> gcc fraction.m -framework Foundation
fraction.m: In function ‘main’:
fraction.m:58: warning: ‘Fraction’ may not respond to ‘-free’
fraction.m:58: warning: (Messages without a matching method signature
fraction.m:58: warning: will be assumed to return ‘id’ and accept
fraction.m:58: warning: ‘...’ as arguments.)

colin@weasel:/tmp> ./a.out
The value of the fraction is 1/3
2009-09-03 08:07:02.800 a.out[693:903] * __NSAutoreleaseNoPool(): Object 0x10010c6e0 of class NSCFString autoreleased with no pool in place - just leaking
2009-09-03 08:07:02.808 a.out[693:903] -[Fraction free]: unrecognized selector sent to instance 0x10010c5c0
2009-09-03 08:07:02.808 a.out[693:903] * __NSAutoreleaseNoPool(): Object 0x100110040 of class NSCFString autoreleased with no pool in place - just leaking
2009-09-03 08:07:02.809 a.out[693:903] * __NSAutoreleaseNoPool(): Object 0x1001101b0 of class NSException autoreleased with no pool in place - just leaking
2009-09-03 08:07:02.809 a.out[693:903] * __NSAutoreleaseNoPool(): Object 0x100110a70 of class _NSCallStackArray autoreleased with no pool in place - just leaking
2009-09-03 08:07:02.810 a.out[693:903] * __NSAutoreleaseNoPool(): Object 0x100110ad0 of class _NSCallStackArray autoreleased with no pool in place - just leaking
2009-09-03 08:07:02.810 a.out[693:903] * __NSAutoreleaseNoPool(): Object 0x100110d90 of class NSCFString autoreleased with no pool in place - just leaking
2009-09-03 08:07:02.887 a.out[693:903] * __NSAutoreleaseNoPool(): Object 0x100111ae0 of class NSCFString autoreleased with no pool in place - just leaking
2009-09-03 08:07:02.888 a.out[693:903] * __NSAutoreleaseNoPool(): Object 0x100110bd0 of class NSConcreteMutableData autoreleased with no pool in place - just leaking
2009-09-03 08:07:02.889 a.out[693:903] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Fraction free]: unrecognized selector sent to instance 0x10010c5c0'
* Call stack at first throw:
(
0 CoreFoundation 0x00007fff861025a4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff85fa5313 objc exceptionthrow + 45
2 CoreFoundation 0x00007fff8615b2a0 +[NSObject(NSObject) doesNotRecognizeSelector:] + 0
3 CoreFoundation 0x00007fff860d530f __forwarding__ + 751
4 CoreFoundation 0x00007fff860d1458 CF_forwarding_prep0 + 232
5 a.out 0x0000000100000d72 main + 187
6 a.out 0x0000000100000bc8 start + 52
7 ??? 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Abort trap
colin@weasel:/tmp>

Sep 2, 2009 11:52 PM in response to etresoft

Even though my other post (more or less) shows how to compile ObjC from the command line, I agree that (in particular a beginner) should use XCode with its templates, and follow the introductory tutorials that are part of Apple's developer documentation, before trying to fiddle around with low-level details of the build process...

Oct 21, 2009 5:39 AM in response to etresoft

Guys, I'm new to Objective-C too and encountered the same error on Snow Leopard. The problem only occurs when the Active Architecture in XCode is set to x86_64. If you flip this over to i386 the example code compiles fine.

I recognize the example code referenced. It's from the same Programming in Objective C book that I'm reading which apparently dates back to the pre-xcode days at Apple (circa. 2002/2003).

Neil

Oct 21, 2009 8:53 AM in response to iNeil

iNeil wrote:
Guys, I'm new to Objective-C too and encountered the same error on Snow Leopard.


I doubt it since the original error was user error.

I recognize the example code referenced. It's from the same Programming in Objective C book that I'm reading which apparently dates back to the pre-xcode days at Apple (circa. 2002/2003).


There are no pre-xcode days. There used to be ProjectBuilder, but that was just an old name for Xcode. The code in question was never correct, not even on day 1. Maybe it worked 6 or 7 years ago, but it doesn't now and no one should be using it as a reference.

Snow Leopard breaks GCC?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.