らんだむな記憶

blogというものを体験してみようか!的なー

Objective-C++サンプル

折角なのでObjective-C++をやる。
sample.{h,m}はそのままとする。んで、以下を追加あるいは変更。

#ifndef ANOTHER_H
#define ANOTHER_H

namespace cpp {

class Another {
 public:
    Another();
    ~Another();

    void echo(const char* msg);
};

} /* namespace cpp */

#endif /* ANOTHER_H */
#include "another.h"
#include <iostream>

namespace cpp {

Another::Another()
{
}

Another::~Another()
{
}

void Another::echo(const char* msg)
{
    std::cout << "*** " << msg << " ***" << std::endl;
}

} /* namespace cpp */

んで、次にmain.mObjective-C++用に拡張子を.mから.mmに変更して、
[main.mm]

#import "sample.h"
#include "another.h"

int main(int argc, char* argv[])
{
    Sample* sample = [[[Sample alloc] init] autorelease];
    [sample echo: "Hello!!!"];

    cpp::Another* another = new cpp::Another;
    another->echo("World!!!");
    delete another;

    return 0;
}

とかしておく。
コンパイラの叩き方が分からないのでこまかいことは忘れて...

$ clang -o sample.o -c sample.m -fobjc-arc
$ clang++ -o another.o -c another.cpp -fobjc-arc
$ clang -o main.o -c main.mm -fobjc-arc
$ clang++ -o test sample.o another.o main.o -framework Foundation -fobjc-arc

でなんとかいけた!
コマンドラインソースコードを逐一オブジェクトファイルへとコンパイルしたのは初めてだなぁ。いつもMakefileにさせてるからなぁ。