Objective-C の環境構築

Windows 用の Objective C

Gnustep は Openstep プロジェクトで開発されたフリーの環境。

インストール

http://www.gnustep.org/windows/installer.html より、 GNUstep MSYS System , GNUstep Core, GNUstep Devel をダウンロードしインストール。 さらに、 SystemPreferencesと Gorm もインストールすると良い。

起動

Start → すべてのプログラム → GNUstep → Shell で対話型端末画面が出る。 UNIX ライクなコマンド操作で操作できる。 ホームフォルダは \GNUstep\msys\1.0\home\ユーザ名 となる。

Hello World

つぎのようなプログラムがあったとする。

helloworld.m



#import <Foundation/Foundation.h>
int main(void){
  NSLog(@"Hello, World");
  return 0;
}

これをコンパイルするには次のような GNUmakefile が必要となる。

GNUmakefile



#
# An example GNUmakefile
#

GNUSTEP_MAKEFILES = C:\GNUstep\GNUstep\System\Library\Makefiles

# Include the common variables defined by the Makefile Package
include $(GNUSTEP_MAKEFILES)/common.make

# Build a simple Objective-C program
TOOL_NAME = helloworld

# The Objective-C files to compile
helloworld_OBJC_FILES = helloworld.m

-include GNUmakefile.preamble

# Include in the rules for making GNUstep command-line programs
include $(GNUSTEP_MAKEFILES)/tool.make

-include GNUmakefile.postamble

この二つのファイルがあれば、 makeコマンドによりコンパイルし、 obj/helloworld.exeで実行できる。

オブジェクト版Hello World

次はオブジェクトを作るバージョン

Hello.h



#import <Foundation/NSString.h>
@interface Hello : NSObject
{
    NSString* message;
}
- (NSString*) world;
@end

Hello.m



#import <Foundation/NSString.h>
#import "Hello.h"
@implementation Hello
- (id) init {
  if(self = [super init]){
    message = [[NSString alloc] initWithString: @"Hello World again"];
  }
  return self;
}
- (NSString*) world {
  return message;
}
- (void) dealloc {
  [message release];
  [super dealloc];
}
@end

helloworldmain.m



#import <Foundation/Foundation.h>
#import "Hello.h"
int main(void){
  id hello = [[Hello alloc] init];
  NSLog(@"%@",[hello world]);
  [hello release];
  return 0;
}

これをコンパイルするには次のような GNUmakefile が必要となる。

GNUmakefile



#
# An example GNUmakefile
#

GNUSTEP_MAKEFILES = C:\GNUstep\GNUstep\System\Library\Makefiles

# Include the common variables defined by the Makefile Package
include $(GNUSTEP_MAKEFILES)/common.make

# Build a simple Objective-C program
TOOL_NAME = hello2

# The Objective-C files to compile
hello2_OBJC_FILES = Hello.m helloworldmain.m

-include GNUmakefile.preamble

# Include in the rules for making GNUstep command-line programs
include $(GNUSTEP_MAKEFILES)/tool.make

-include GNUmakefile.postamble


この二つのファイルがあれば、 makeコマンドによりコンパイルし、 obj/hello2.exeで実行できる。

Debian 系

  1. sudo apt-get install gobjc libgnustep-base-dev gnustep gnustep-devel
  2. そして以下のようにgccコマンドを実行する事でコンパイル出来る。 gcc test.m -lobjc -lgnustep-base -I/usr/include/GNUstep

参考文献

  1. http://d.hatena.ne.jp/tomute/20090112/1231809116