objective-c - 如何在 Linux 中运行简单的 Objective-C

标签 objective-c linux gcc terminal

已编辑

我一直在尝试开始使用 Objective-c 进行编码。它只是一个尝试 getter 和 setter 方法的简单程序。还打印 Hello World。以下是我的代码:

#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");
}

我现在得到垃圾

/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

我使用了命令gcc -o hello hello.m -lobjc

我花了几个小时在谷歌上搜索这个答案。

最佳答案

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

#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;
}

关于objective-c - 如何在 Linux 中运行简单的 Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19734792/

相关文章:

ios - 如何在 ios 中使用 google Analytics,而无需在所有库中扩展库 GAY Tracked ViewController

linux - 关于为 Linux 创建内核模块的一些文档/书籍?

c++ - 在 g++ 中删除此指针行为

objective-c - 从 C# 背景学习 iPhone 编程

ios - 索引日期类型的核心数据属性是否有意义?

ios - UITableView 不显示 IOS 7 中的单元格

c++ - 将以 Windows 为中心的控制台 I/O 移植到 Linux

linux - 如何确保用户输入有效的 switch case 选项?

c - 使用 GCC 驱动程序时,什么使静态库成为 "incompatible"?

c++ - 匿名命名空间在这里导致 undefined reference - 在那里工作