Objective-Cに触れてみる
・Objective-Cに必要なものをaptしてみる。
aptメモ
gobjc gobjc-multilib
・Objective-Cのソースをみてみる。
#import @interface @implementation @end [] id
見慣れる単語に戸惑う。
・とりあえずソースを写経してみる。
単語@implementationに戸惑う。
インプリメンテーションインプリメンテーションインプリメンテーション…
・コンパイルしてみる。
gcc -C main.m 致命的エラー: Foundation/NSObject.h: そのようなファイルやディレクトリはありません
・問題を検索する。
以下をaptすればよいようだ。
aptメモ
autotools-dev gnustep-base-common gnustep-base-runtime gnustep-common gnustep-make libgnustep-base-dev libgnustep-base1.22
・再度コンパイルしてみる。
gcc -C main.m -I/usr/include/GNUstep エラー: cannot find interface declaration for ‘NXConstantString’
写経したソース内で「@"文字列"」を使うとエラーが出るみたいだよ
文字列定数クラスとは何ですか @"Hello world!"のようなObjective-Cの文字列リテラルを 使用する場合、その文字列オブジェクトのクラスは通常NXConstantString になります。この文字列クラスは、コンパイル時に gccの-fconstant-string-classオプションを使用して変更することが できます。Cocoaでは、文字列リテラルのクラスはNSConstantStringに なります。もし、文字列定数クラスを別のものに変更したいなら、そのクラスには どのようなメソッドを実装してもよいですが、インスタンス変数のレイアウトは、 メモリ上のレイアウトをコンパイラが実行ファイルに書き込むので、 N[SX]ConstantStringと同じにしなければなりません (objc/NXConstStr.h、または、Foundation/NSString.hを参照)。
・再々度コンパイル
gcc -C main.m -I/usr/include/GNUstep -fconstant-string-class=NSConstantString main.m:(.text+0x12): undefined reference to `objc_get_class' main.m:(.text+0x24): undefined reference to `objc_msg_lookup' main.m:(.text+0x43): undefined reference to `objc_msg_lookup' main.m:(.text+0x60): undefined reference to `objc_get_class' main.m:(.text+0x72): undefined reference to `objc_msg_lookup' main.m:(.text+0x91): undefined reference to `objc_msg_lookup' main.m:(.text+0xb6): undefined reference to `objc_msg_lookup' main.m:(.text+0xdf): undefined reference to `objc_msg_lookup' main.m:(.text+0x108): undefined reference to `objc_msg_lookup' /tmp/ccmAwX03.o: In function `__objc_gnu_init': main.m:(.text+0x131): undefined reference to `__objc_exec_class' /tmp/ccmAwX03.o:(.data+0x70): undefined reference to `__objc_class_name_Song' /tmp/ccmAwX03.o:(.data+0x74): undefined reference to `__objc_class_name_Singer' /tmp/ccmAwX03.o:(.data+0x78): undefined reference to `__objc_class_name_NSConstantString' collect2: ld はステータス 1 で終了しました
・4度目コンパイル
リンクオプションをつける。
gcc -C main.m -I/usr/include/GNUstep -fconstant-string-class=NSConstantString -lgnustep-base
・実行してみる。
./a.out 2012-12-26 17:20:00.189 a.out[9832] autorelease called without pool for object (0x9bf2790) of class GSAutoreleasedMemory in thread <NSThread: 0x9bc2498>
・ソースを編集する。
以下を追記することによりメッセージがでなくなった。
詳しいことはあとあと調べよう。
#import <Foundation/NSAutoreleasePool.h> … id pool = [[NSAutoreleasePool alloc] init];
・スタートラインに立つ
ここからやっとこさObjective-Cに触れて行けそうである。