0

EDITED

I have been trying to start coding in Objective-c. Its just a simple program to try getter and setter methods. Also print Hello World. THe following is my code:

#import <objc/Object.h>

@interface Car:Object{
   int wheel: 5;
}

- (int)wheel;
- (void)setWheel: (int)newWheel;

@end

#include <stdio.h>

@implementation Car

- (int)wheel{
     return wheel;
}


- (void)setWheel: (int)newWheel{
     wheel = newWheel;
}
@end

#include <stdlib.h>
int main(void){
printf("Hello World");
}

I now get garbage

/tmp/cc3UC6jY.o: In function `__objc_gnu_init':
    hello.m:(.text+0x6d): undefined reference to `__objc_exec_class'
     /tmp/cc3UC6jY.o:(.data+0x1c0): undefined reference to `__objc_class_name_Object'
   collect2: error: ld returned 1 exit status

I used the the command gcc -o hello hello.m -lobjc

I have spent hours googling this answer.

4

1 回答 1

0

您的代码的以下变体为我编译并运行:

#import <objc/Object.h>

@interface Car : Object {
   int wheel;
}

- (int)wheel;
- (void)setWheel: (int)newWheel;

@end

@implementation Car
  - init {
    wheel = 5;
    return self;
  }

  - (int)wheel {
    return wheel;
  }

  - (void)setWheel: (int) newWheel {
    wheel = newWheel;
  }
@end

#include <stdio.h>

int main(void){
  printf("Hello World\n");
  id myCar = [[Car alloc] init];
  printf("Wheel value is %d\n", [myCar wheel]);
  return 0;
}
于 2013-11-01T22:20:47.850 回答