Stu
Stu

Reputation: 2699

Compiling an Objective-C program

I'm having problems compiling the following program. I'm using "gcc -framework Foundation inherit8.1m" and get the following errors. What am I doing wrong? Thanks.

ld warning: in inherit8.1m, file is not of required architecture Undefined symbols: "_main", referenced from: start in crt1.10.5.o ld: symbol(s) not found collect2: ld returned 1 exit status

// Simple example to illustrate inheritance


#import <Foundation/Foundation.h>

// ClassA declaration and definition

@interface ClassA: NSObject
{
   int  x;
}

-(void) initVar;
@end

@implementation ClassA
-(void) initVar
{
  x = 100;
}
@end

// Class B declaration and definition

@interface ClassB : ClassA
-(void) printVar;
@end

@implementation ClassB
-(void) printVar
{
  NSLog (@"x = %i", x);
}
@end

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

   ClassB  *b = [[ClassB alloc] init];

   [b initVar];     // will use inherited method
   [b printVar];    // reveal value of x;

   [b release];

   [pool drain];
   return 0;
}

Upvotes: 0

Views: 198

Answers (3)

stefanB
stefanB

Reputation: 79770

I found it easier to use GNUmakefile on linux (not sure if this is your case). I have a command line tool LogTest compiled from source.m:

> cat source.m
#import <Foundation/Foundation.h>

int main(void)
{
    NSLog(@"Executing");
    return 0;
}

> cat GNUmakefile
include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = LogTest
LogTest_OBJC_FILES = source.m

include $(GNUSTEP_MAKEFILES)/tool.make

> make
Making all for tool LogTest...
 Compiling file source.m ...
 Linking tool LogTest ...

> ./obj/LogTest
2009-05-17 20:05:36.032 LogTest[9850] Executing

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299265

You've misnamed your file. It should be inherit8.m, not inherit8.1m.

Upvotes: 1

Paul M
Paul M

Reputation: 56

Try renaming your source file to something that ends in just .m. Your file has an extension of .1m which appears to confuse the compiler.

Upvotes: 4

Related Questions