Apple Event: May 7th at 7 am PT

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

Nov 2, 2009 12:50 AM in response to Kostantino

Sure does! Thanks Constantino!

Now in my .tcshrc!
alias cc gcc -arch i386
alias gcc gcc -arch i386

Was evil not to have that as a default. Just what is the default architecture? From the gcc man page:

-arch arch
Compile for the specified target architecture arch. The allowable values are i386, x86_64, ppc
and ppc64. Multiple options work, and direct the compiler to produce "universal" binaries
including object code for each architecture specified with -arch. This option only works if
assembler and libraries are available for each architecture specified. (APPLE ONLY)

Mar 11, 2010 9:00 PM in response to hogstrom

Using the same book and ran into the same issues. It bothered me that readers like myself were only able to compile in i386 (32bit) and thought that could really hurt later when trying to compile stuff in Xcode for a 64bit native application.

The good news is, I was able to get the code to compile in both 64bit and 32bit modes. 64bit requires changing the 'Object' class to subclass from the NSObject class instead. Otherwise you will get a NSInvocation error at the Objective-C runtime with the 64bit binary. Reference and better explanation for the fix: http://www.thepursuitofquality.com/Post/27/FixingNSInvocationErrors.html

I noticed that there is a 'print' method in the example, but it is not used; enabled it in my revision. Other complications: The numerator and deminator methods in the book's example are redundant; these variables are already set in the setNumerator and setDeminator methods. I think it was odd that the book calls the numerator and deminator and returns the same value; maybe they were trying to show that these objects could message themselves. Anyways those unnecessary calls were removed to keep it simple. Also when using the Foundation framework, 'Object.h' and the 'stdio.h' headers are unnecessary for Mac OS X. To stay more compliant with Cocoa, used NSLog instead of 'printf' for the output.

Below is an updated example that compiles in Xcode 3.2.1 as a Command Line Tool, using the 'Foundation' Library template.


// fraction.m revision for Snow Leopard by NiceMac LLC
// This will compile in the command line as well with:
// $ gcc fraction.m -o fraction -framework Foundation
// $ ./fraction
#import <Foundation/Foundation.h>
/* Interface */
@interface Fraction: NSObject
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) dealloc;
@end
/* Implementation */
@implementation Fraction;
-(void) print
{
NSLog (@"The value of the fraction is %i/%i", numerator, denominator);

}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(void) dealloc {
NSLog(@"Release called. Deallocating memory...");
[super dealloc];
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction = [[Fraction alloc] init];

// Set fraction to 3/4
[myFraction setNumerator: 3];
[myFraction setDenominator: 4];

// the 'print' method was never called, decided to put the message there
// Display the fraction
[myFraction print];

//avoid memory leak with release and super delloc
[myFraction release];
[pool drain];
return 0;
}


result

[Session started at 2010-03-11 23:21:32 -0500.]
...
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001
Loading program into debugger…
Program loaded.
run
[Switching to process 43733]
Running…
2010-03-11 23:21:33.302 Test[43733:a0f] The value of the fraction is 3/4
2010-03-11 23:21:33.306 Test[43733:a0f] Release called. Deallocating memory...
Debugger stopped.
Program exited with status value:0.


Hope this helps anyone learning to code using the Learning Objective-C book by Stephen G Kochan.

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.