我在 Linux(Ubuntu) 中学习 Objective-C,但是当我尝试编译需要 Foundation 标头的应用程序时,出现错误,提示找不到该文件,但我已经安装了 GNUstep 开发包(gnustep-devel)。这是我的代码:

// Fraction.h
#import <Foundation/NSObject.h>

@interface Fraction: NSObject {
    int numerator;
    int denominator;
}

- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;
- (void) numerator;
- (void) denominator;
@end

这是控制台日志:

ubuntu@eeepc:~$ gcc main.m -o frac -lobjc
In file included from main.m:3:
Fraction.h:2:26: error: objc/NSObject.h: No such file or directory
In file included from main.m:3:
Fraction.h:4: error: cannot find interface declaration for ‘NSObject’, superclass of ‘Fraction’
ubuntu@eeepc:~$

我需要做什么?

有帮助吗?

解决方案

Windows 下的 GNUstep 安装过程

  1. 访问网址: http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/
  2. 下载exe文件gnustep-system-0.19.2-setup.exe
  3. 然后下载gnustep-core-0.19.2-setup.exe

请记住一件事,如果您要下载任何版本的 gnustep-system,则必须下载相同版本的 gnustep-core。例如,如果您已经下载了 gnustep-setup-0.22.1-setup.exe,那么您必须下载 gustep-core-0.22.1-setup.exe,否则您的代码将无法运行。

  1. 首先安装 gnustep-system-0.19.2-setup.exe,然后安装 gnustep-core-0.19.2setup.exe。不要尝试以相反的顺序安装。
  2. 现在你已经获得了 Windows 版的 gnustep,然后转到开始>所有程序> GNUstep> shell
  3. 现在打开记事本编辑器并在记事本中编写以下代码
#import  
int main (int argc, const char * argv[])
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]  
init];
  NSLog (@"Anil Kumar Yadav has Run the First Objective C  
program!");
  [pool drain];
  return 0;
}

将其另存为hello.m。文件夹。所以不要惊慌。好的

  1. 转到 shell 并键入以下命令 gcc -o hello hello.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -constant-string-class=NSConstantString
  2. 此命令将在您的foldername 文件夹中创建一个hello.exe 文件。
  3. 再次在 shell 中输入命令 ./hello.exe

最后你将能够在 shell 中看到输出。

恭喜您成功编写了第一个 Objective C 程序。需要任何澄清请写信给我:aayadav00009@gmail.com

scroll top