7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MacのターミナルでObjective-Cをコンパイルする方法

Posted at

「新言語Swift! ObCに比べて読みやすい! iOSアプリ開発始めるぜえええ!!」と意気込んでいた一ヶ月ほど前の私ですが、まだ世の中にはObjective-Cで書かれたライブラリや情報が多く、SwiftだけでなくObjective-Cも文法ぐらいは理解する必要があるとようやく悟りました。

だいたいの文法を頭に入れるためにコマンドラインで練習をしようとするも、なかなかコンパイルが成功せず。つまった・・・。

今回進めたかったのはこちら↓ ソースコードも少し引用させていただいています。

一番初めのObjective-Cプログラム (3/3)
http://www.atmarkit.co.jp/ait/articles/0810/08/news117_3.html

Singer.h, Singer.m, Song.h, Song.m, main.mを作成してコンパイルします。main.mだけ載せておきます。

main.m
# import "Song.h"
# import "Singer.h"

int main(void) {

    id song;
    id singer;

    song = [[Song alloc] init];
    [song setLyrics:@"それが僕達の奇跡"];
    
    singer = [[Singer alloc] init];
    [singer setSong:song];
    [singer sing];

    return 0;
}

Foundationの場所を絶対パスで指定したらできた

私ははじめ、多くのチュートリアルと同様に以下のようにimportしておりました。

Singer.m
# import <Foundation/NSObject.h>
# import <stdio.h>
(略)

するとコンパイルできず。

$ gcc -c main.m

-framework Foundationをつけてみたり、gccの最新版を入れてみたり、clangに変えてみたりしましたが効果なし。

「そもそもどこにあるんじゃあああ」とGoogle先生に叫んだら、ここにFoundationがどこに入ってるか書いてあると教えてくれました。

Foundation Framework Reference
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/ObjC_classic/_index.html

力技で美しくないけれどとりあえずこれでコンパイルは通るようになりました。

$ gcc -c main.m
$ gcc -c Song.m
$ gcc -c Singer.m
$ gcc -o myFirstProgram main.o Song.o Singer.o -framework Foundation

ちなみにgccをそのままclangに変えてもいけます。clangの方が王道みたいですね。∑

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?