Boost_Install - アールメカブ

アールメカブ


Boost_Install

Programming

_ Mac の場合

いろいろ情報があって,どれが「正当」なのかわからん. とりあえず以下のように処理

imac:~ motohiro$ cd progSource/boost-jam-3.1.11
imac:boost-jam-3.1.11 motohiro$ ./build.sh 
...
cd ../boost_1_33_1
imac:boost_1_33_1 motohiro$ ./bjam -sTOOLS=darwin
          --prefix=/Users/motohiro/Boost install

とりあえず以下のメッセージは無視

*** If you don't need Boost.Python, you can ignore this section   ***
*** pass --without-python to suppress this message in the future ***

skipping Boost.Python library build due to missing or
 incorrect configuration

couldn't find Python.h in "/System/Library/Frameworks 
/Python.framework/Versions/2.4/include/python2.4"

You can configure the location of your python installation
 by setting:
PYTHON_VERSION - The 2-part python Major.Minor version number 
(e.g.   "2.2", NOT "2.2.1")  - currently "2.4"

PYTHON_ROOT    - currently "/System/Library/Frameworks
/Python.framework/Versions/2.4"

The following are automatically configured from PYTHON_ROOT 
 if not otherwise set:

   PYTHON_LIB_PATH       - path to Python library object; 
currently 
   PYTHON_INCLUDES       - path to Python #include directories; 
currently
                           "/System/Library/Frameworks 
/Python.framework/Versions/2.4/include/python2.4"
------------------------------------------------------------------
Building Boost.Regex with the optional Unicode/ICU support  
disabled.
Please refer to the Boost.Regex documentation for more information
(and if you don't know what ICU is then you probably don't need it).

...patience...

これでヘッダファイル系の Boost/include/boost-1_33_1 と ライブラリ系の Boost/lib というフォルダが作成される. ヘッダはシンボルを張る

 motohiro$ sudo ln -s /Users/motohiro/Boost/include/boost-1_33_1/boost 
        /usr/local/include/boost

ライブラリの方は,/usr/local/lib に全部突っ込んでも良いのだが,さしあたり make で追加の指定を行う.ただシンボリックリンクは張っておく.

 motohiro$ sudo ln -s /Users/motohiro/Boost/lib
               /usr/local/lib/boost

たとえばここになどがあり,他に ここにはこうしろとあり,

まず boost.org からboost と bjam をゲットする。 bjam は boost を展開した直下に置く。で、boost を展開したディレクトリに入る。

% ./configure --with-bjam=./bjam
% make

あるいはこうしろという情報もある

以前書いた引数では Boost 1.35.0 はビルドできなかったので、ちょっと修正。

bjam link=static threading=multi --with-thread --with-filesystem release debug stage

runtime-link に static を設定できなくなったようなので、 link に変更。それだけではデバッグ版がビルドされなかったので、 stage の前に release と debug を追加。一応これでライブラリは出来たが……そもそも 1.35.0 では gcc 3.2.3 は公式サポートから外されているんだよねぇ。

  • Mac port の場合
    sudo port install boost
     ## error が起こったら,
     ## sudo port clean boost
     ## % sudo port install jam
     ## % sudo port install boost
     ## とやり直せばいいようだ

だが,この後

export PATH=/opt/local/bin:/opt/local/sbin/:$PATH
export MANPATH=/opt/local/man:$MANPATH
export LIBRARY_PATH=/opt/local/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/opt/local/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=/opt/local/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/opt/local/include:$CPLUS_INCLUDE_PATH
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
export BOOST_ROOT=/opt/local/include/boost:$BOOST_ROOT

と環境変数の設定が必要.さもなければ

$ g++ main.cpp -I/opt/local/include/boost /opt/local/lib/libboost_regex-mt.a
$ g++ main.cpp -I/opt/local/include /opt/local/lib/libboost_regex-mt.dylib

などと指定してやる必要がある.

_ Windows の場合.

  • ダウンロードした boost_1_33_1.zip と boost-jam-3.1.11-1-ntx86.zip を temp フォルダに解凍し,jam.exe を移動して,次を実行.
C:\temp\boost_1_3_11> bjam -sTOOLS=mingw  install

を実行し,ヘッダとライブラリのまとまったフォルダ

C:/Boost

が作成される を作成

  • RCaBoCha/src/Makevars.win に以下を追記
PKG_CPPFLAGS = -I. -Ic:/Boost/include/boost-1_33_1
# PKG_LIBS = -Lc:/Program\ Files/CaBoCha/bin $(MECAB)
             -Lc:/Boost/lib/libboost_regex-mgw.lib # (予備)

_ インストール

以下,ここから引用

  • Boost 1.33.1 の入手

BoostはBoostのホームページのダウンロードページ からダウンロードできます。SourceForge?よりのダウンロードになります。

File Releases から boost_1_33_1.zip と boost-jam-3.1.11-1-ntx86.zip をダウンロードします。 boost_1_33_1.zip は boost本体です。boost_1_33_1.exe(自己解凍形式の圧縮ファイル)でもかまいません。

boost-jam-3.1.11-1-ntx86.zip は bjam.exe が入っており、インストール時に利用するツールになります。

  • 入手した圧縮ファイルの解凍

boost_1_33_1.zip を適当な場所に解凍します。 指定したフォルダ下に boost_1_33_1 フォルダが作成され展開されます。

次に boost-jam-3.1.11-1-ntx86.zip を適当な場所に解凍します。

解凍したフォルダ内に bjam.exe がありますので boost_1_33_1.zip を解凍したホームフォルダ boost_1_33_1 下にコピーします。

  • Boost 1.33.1 のインストール

テンプレートで提供されている機能だけの利用であれば、 上記で展開したホームフォルダにインクルードパスを通すことで利用できます。

今回は regex を利用したいので bjam を使用してインストールします。 また、Boost.Pyhton を利用する場合は 別途 Python を導入しておく必要があります。 ここでは Boost.Pyhton は利用しません。

尚、インストールの詳しい説明はGetting Startedを参照してください。

では、bjam を使って Boost をビルド、インストールします。DOSプロンプトで Boostを展開したホームフォルダに移動します。

bjam は以下の形式になります。

bjam -sTOOLS=処理系名 --prefix=インストール先 install

以下で 処理系名は mingw 、インストール先は d:\Boost としコマンドを入力します。--prefixを省略した場合のデフォルト値は c:\Boost になります。

bjam -sTOOLS=mingw --prefix=d:\Boost install

ビルド、インストールは時間がかかります。 bjam が完了すれば完了です。Boost.Pyhton等、いくつかのライブラリは生成されません。 インストール先の libs の下に各ライブラリが生成され、 include\boost-1_33_1\boost の下にヘッダファイルが生成されています。 (include の下に boost-1_33_1\boost フォルダが作成され展開されています。)

_ コンパイルとリンク

  • ヘッダファイルのインクルード

Boost.Regex を利用する場合は、boost/regex.hpp をインクルードする必要があります。

ソースに以下のように記述します。

#include <boost/regex.hpp>

インクルードパスの設定

コンパイルする際、インクルードパスを設定します。

「Boostのインストール」で説明した方法でインストールした場合、 インストール先のフォルダ下のinclude\boost-1_33_1\boost の下にヘッダファイルが生成されています。

Boostのヘッダファイルの #include の記述は #include <boost/xxxxx.hpp> となっていますので、 インクルードパスの指定は <インストール先のフォルダ>\boost-1_33_1 となります。

例えば C:\Boost にインストールした場合は、以下のようにコンパイル時に指定します。

-Ic:/Boost/include/boost-1_33_1
  • regexライブラリのリンク

次にライブラリをリンクするように指定します。

インストール先のフォルダ下の lib の下に各ライブラリが作成されています。

regex*の名前のものがregexのライブラリです。DLLも生成されていますが、今回は静的ライブラリを利用します。

libboost_regex-mgw.lib が静的ライブラリになりますので、本ライブラリを静的リンクします。

以下に、Makefileの例を記述します。

     CXX = g++
CXXFLAGS = -g
 LDFLAGS = 
INCLUDES = -I. -Ic:/Boost/include/boost-1_33_1
    LIBS = c:/Boost/lib/libboost_regex-mgw.lib
  TARGET = sample1.exe
    SRCS = sample1.cpp
    OBJS = $(SRCS:.cpp=.o)

all: $(TARGET)
clean:
	-rm $(TARGET) $(OBJS)
$(TARGET): $(OBJS)
	$(CXX) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
.cpp.o:
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
  • 正規表現の利用 regex_match

regex_match は入力テキストが正規表現と完全に一致、またはオプションにより途中まで一致した場合に、 結果が真になります。

基本的な使用例を記述します。

以下のコードは、文字列 "Free Programming For Windows." に対して、正規表現 "Free.+Windows\." が完全一致するか検査しています。

\は""内でエスケープ文字となりますので\\と記述します。

string str("Free Programming For Windows.");
boost::regex reg("Free.+Windows\\.");

if (boost::regex_match(str, reg)) {
	cout << "===> Matched." << endl;
} else {
	cout << "===> Not matched." << endl;
}

結果は真となり、以下のように表示されるはずです。

===> Matched.

次に、以下のコードではどうでしょうか?

string str("Free Programming For Windows.");
boost::regex reg("Free.+Windows");

if (boost::regex_match(str, reg)) {
	cout << "===> Matched." << endl;
} else {
	cout << "===> Not matched." << endl;
}

正規表現に先後の.の一致がありませんので、結果は偽となり、以下のように表示されるはずです。

===> Not matched.

このように、regex_match は正規表現と完全に一致した場合に真となります。

では、正規表現はそのままに以下のように regex_constants::match_partial を指定してみます。

string str("Free Programming For Windows.");
boost::regex reg("Free.+Windows");

if (boost::regex_match(str, reg,  boost::regex_constants::match_partial)) {
	cout << "===> Matched." << endl;
} else {
	cout << "===> Not matched." << endl;
}

regex_constants::match_partial を指定すると、途中まで一致した結果は真となり、 以下のようになります。

===> Matched.

次に match_results を利用して、マッチした値を取得します。

string str("Free Programming For Windows.");
boost::regex reg("Free (\\w+) (\\w+) Windows\\.");

boost::smatch result;
if (boost::regex_match(str1, result, reg1c)) {
	cout << "===> Matched." << endl;
	cout << "size():" << result.size() << endl;
	cout << "result.str(0):" << result.str(0) << endl;
	cout << "result.str(1):" << result.str(1) << endl;
	cout << "result.str(2):" << result.str(2) << endl;
} else {
	cout << "===> Not matched." << endl;
}

match_resultsは正規表現でマッチした結果の集合を示します。 0番目の要素はマッチした全体を示し、1番目は最初の()で囲まれたグループを示し、 2番目は次の()で囲まれたグループを示します。

結果は以下のようになります。

===> Matched.
size():3
result.str(0):Free Programming For Windows.
result.str(1):Programming
result.str(2):For

_ Boost.Regexを使ってみる(1)

今回はMinGW環境下でBoost.Regexを使ってみます。 Boost.Regexはテンプレートだけではなく、ライブラリを必要としますのでリンクでregexのライブラリをリンクする必要があります。

  • 正規表現の利用regex_search

regex_search は入力テキストに正規表現に一致するものが含まれている場合に、結果が真になります。 また、regex_matchとは異なり全ての一致を探します。

基本的な使用例を記述します。

以下のコードは、文字列 "Free <em>Programming</em> For Windows." に対して、正規表現 "<[^>]+>" が一致するか検査しています。

string str("Free <em>Programming</em> For Windows.");
boost::regex reg("<[^>]+>");

if (boost::regex_search(str, reg)) {
	cout << "===> Matched." << endl;
} else {
	cout << "===> Not matched." << endl;
}

結果は真となり、以下のように表示されるはずです。

===> Matched.

次の例は、一致した文字をすべて取得し表示します。

string str("Free <em>Programming</em> For Windows.");
boost::regex reg("<[^>]+>");
string::const_iterator start=str.begin();
string::const_iterator end=str.end();
boost::smatch result;
while (regex_search(start, end, result, reg)) {
	cout << result.str(0) << endl;
	start = result[0].second;
}

結果は以下のように表示されます。

<em>
</em>

以下は同時に()でグルーピングした値を取得しています。

string str("Free <em>Programming</em> For Windows.");
boost::regex reg("<([^>]+)>");
string::const_iterator start=str.begin();
string::const_iterator end=str.end();
boost::smatch result;
while (regex_search(start, end, result, reg)) {
	cout << result.str(0) << endl;
	cout << result.str(1) << endl;
	start = result[0].second;
}

結果は以下のように表示されます。

<em>
em
</em>
/em
  • regex_replace

regex_replace は正規表現に一致するものを置換します。

基本的な使用例を記述します。

以下のコードは、文字列 "Free Programming For Windows." に対して、正規表現 "P\w+" に一致するものを"Software"に置換します。

string str("Free Programming For Windows.");
boost::regex reg("P\\w+");
string str2 = regex_replace(str, reg, "Software");
cout << str2 << endl;

結果は、以下のように表示されます。

Free Software For Windows.

置換文字列には$0等でマッチした値を使用することができます。

string str("Free Programming For Windows.");
boost::regex reg("P\\w+");
string str2 = regex_replace(str, reg, "<em>$0</em>");
cout << str2 << endl;

結果は、以下のように表示されます。

Free <em>Programming</em> For Windows.
 
Last-modified: 2010-04-29 (木) 14:39:25 (5103d)