xcodeでboost使うためのメモ

非常に忘れっぽいので、というか毎回ググって誰かのブログから見てくるのがめんどくさいので、ここに書いておきます

例えば次のように関数を「nx^m」と書いたときのmが知りたいときってあるじゃないですか(?)。で、正規表現使いたいって思ってboostを読み込むわけですが、ちょっとめんどくさいので、書き残しておきます

regextest.h
#include <iostream>
#include <string>
#include <boost/regex.hpp>

using namespace std;
class RegexTest {
    
public:
    static string getDegree(string func_str);
};
regextest.mm
#include "regextest.h"

string RegexTest::getDegree(string func_str)
{
    boost::regex reg("(\\d*)x\\^(-?\\d+)");
    boost::smatch result;
    if (boost::regex_match(func_str, result, reg)) {
        return result.str(2);
    }
    return NULL;
    
}
main.mm
#import <Cocoa/Cocoa.h>
#include "regextest.h"

int main(int argc, char *argv[])
{
    string f("2x^3");
    cout << "("<< f << ")'s degree is " <<RegexTest::getDegree(f) << endl;
    return NSApplicationMain(argc, (const char **)argv);
}

これ普通にやろうとすると、リンクできないので、
f:id:moratorium08:20131106091148p:plain
このようにHeader Search Path(Build Settings の真ん中のあたりにSearch Pathという項目がありその中にあるやつ)を変更したあと、Build Phasesタブに移って、Link Binary With Librariesで、プラス押すとワーっとライブラリ押すといろいろ出てきますが、その中には無いので、左下のAdd Other押すと、ファイル全体のブラウジングできます。で、僕は/opt/local/lib/にいろいろあるので、そっから、今回使ったlibboost_regex-mt.aを選ぶと
f:id:moratorium08:20131106091613p:plain
実行してみると
f:id:moratorium08:20131106092800p:plain
できました。めでたしめでたし。(というか、まぁmain.mmで出来るので、他のところでもできますねってことですね)

あ、あといつもObjective-C++使うと忘れるのですが、main.mをmain.mmにするのを忘れるな と未来の自分に言っておきます。