Unable to compile simple Objective-C app.

cron0

Registered
Hello,

I am following a guide found on the internet and I am unable to compile it. Following are me 3 source code files:

Fraction.h
Code:
#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

Fraction.m
Code:
#import "Fraction.h"
#import <stdio.h>

@implementation Fraction
-(void) print {
	printf("%i/%i", numerator, denominator);
}

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

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

-(int) denominator {
	return denominator;
}

-(int) numerator {
	return numerator;
}
@end

main.m
Code:
#import <stdio.h>
#import "Fraction.h"

int main( int argc, const char *argv[] ) {
    // create a new instance
    Fraction *frac = [[Fraction alloc] init];

    // set the values
    [frac setNumerator: 1];
    [frac setDenominator: 3];

    // print it
    printf( "The fraction is: " );
    [frac print];
    printf( "\n" );

    // free memory
    [frac release];

    return 0;
}

Compiling:
Code:
gcc Fraction.m main.m -o myapp

Error produced:
Code:
/usr/bin/ld: Undefined symbols:
.objc_class_name_NSObject
_objc_msgSend
collect2: ld returned 1 exit status


Any ideas? Please? :)

Regards,
Jeff
 
I just realized that it compiles fine under Xcode.

Should I use something else than gcc to compile from command line? Should I use different options?

Regards,
Jeff
 
Yes, the errors are repeated here as well.

I tried to understand 'gcc', and wrote numerous 'gcc ...' command lines; but, continuously failed.

What I did to compile and execute the above code:
01. Launched 'Xcode', and created a new 'Command Line Utility - Foundation Tool' project. I named the project 'demo'.
02. In the 'demo.m' file I pasted your 'main.m' code, less the '#import <stdio.h>' line. See the 'demo.m' code below.

'demo.m' code:
Code:
#import <Foundation/Foundation.h>
#import "Fraction.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // create a new instance
    Fraction *frac = [[Fraction alloc] init];

    // set the values
    [frac setNumerator: 1];
    [frac setDenominator: 3];

    // print it
    printf( "The fraction is: " );
    [frac print];
    printf( "\n" );

    // free memory
    [frac release];


    [pool release];
    return 0;
}

03. Next the 'Fraction.h' and 'Fraction.m' files were dragged to the project window's 'Source' folder (in the left hand side list).
04. The 'Build and Go' icon button was clicked on. The code compiled and executed.
05. From the 'Project, Set Active Build Configuration' menu item's sub-menu - the 'Release' menu item was selected; and, then the 'Build and Go' icon button was clicked on. (By default, the build configuration is set to 'Debug'.)

The results of clicking on the 'Build and Go' icon button:

Code:
[Session started at 2006-04-01 07:42:48 -0500.]
The fraction is: 1/3

demo has exited with status 0.
 
Yes as I said it compiles fine under Xcode.

Are there any ways to see what Xcode did execute under the hood to compile the source files?

Jeff
 
You'll need to pass the flag -framework Foundation and perhaps other flags that I can't remember off the top of my head.
 
Viro said:
You'll need to pass the flag -framework Foundation and perhaps other flags that I can't remember off the top of my head.

Wow that worked! Thank you very much!

As much as I find Xcode to be very useful for big projects or GUI applications, I think it is kind of cumbersome for small console applications.

Thanks again!
 
'You'll need to pass the flag -framework Foundation ...' - nice catch Viro.
I was stuck, in a loop, trying to get '-l ...' to work.

'Yes as I said it compiles fine under Xcode.' [cron0] - yes, 'after' I took the time to do what I did (look into the code, why the error messages, how to configure 'gcc', etc.), then submitted my reply (of a workable solution), and then the web page refreshed - did I note you came to the same conclusion and posted it.
 
Back
Top