CLionでBoost
最近、JetBrainsという所が作ってるCLionというC/C++のIDEを導入した。
学生ならば*1一年間無料で利用できる。学生特権バンザイ。www.jetbrains.com
CLlionではCMakeとMinGWを使うので、これらもインストールする必要がある。
今回はCLionでBoostを使うための手順を書いていく。
Windowsにboostを入れる
boostをダウンロード
まずは、Boostを本家からダウンロードしてくる必要がある。
最新版で良い。
ダウンロードしたら、展開して好きな場所に置く。(僕は D:\boost に置いた。)
ビルド
boostはそのままでも使えるらしいが、一部はビルドしないと使えないらしい。
ビルドは主にVisual Studioを使ったビルドとMinGWを使ったビルドがある。
今回はMinGWを使ったビルドを行ったのでその方法を書く。
とは言っても何も大したことではなく、まずコマンドプロンプトを開いてBoostを置いたディレクトリまで移動する。
ここで下記コマンドを入力して実行
bootstrap.bat gcc
これで、b2.exeなる実行ファイルが出来上がる。
この実行ファイルが出来上がったら続いて下記コマンドを入力して実行する。
このコマンドでprefixの値は自分がライブラリを展開したい場所、-j 12はビルド時の並列処理のスレッド数なので各自自分のマシンに合わせた値を入れる。
b2.exe install --prefix=D:\boost\stage toolset=gcc link=static,shared threading=multi variant=debug,release runtime-link=shared -j 12
しばらくすると(ぼくの場合)D:\boost\stageにライブラリが出来上がる。
CLionでBoostを読めるようにする。
…書こうとしたけど、微妙に上手く行ってないから上手くいった部分まで。*2
CMakeLists.txt編集
cmake_minimum_required(VERSION 3.3) project(cpptest) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(SOURCE_FILES main.cpp) add_executable(cpptest ${SOURCE_FILES}) include_directories(D:/boost/stage/include/boost-1_59) target_link_libraries(cpptest ${Boost_LIBRARIES})
という感じで
- include_directories()にヘッダーファイルの場所
- target_link_libraries()にリンクしたいライブラリ
を書く。
ソースコードは [C++] Windows での Boost 環境構築まとめ (Visual Studio 2012, MinGW) - クソネミ( ˘ω˘ ) から拝借した物を使ってみた。
#include <iostream> #include <boost/version.hpp> using namespace std; int main() { cout << (BOOST_VERSION / 100000) << "." << (BOOST_VERSION / 100 % 1000) << "." << (BOOST_VERSION % 100) << endl; return 0; }
実行結果
C:\Users\Home\.CLion12\system\cmake\generated\ba481e8e\ba481e8e\Debug\cpptest.exe 1.59.0 Process finished with exit code 0
ちゃんと正しいバージョンが出てる。
CLionでBoostがちゃんと動かない
「何を言っているんだこいつは…」と思うかもしれないけれど、動かなかったのだ。
詳しく言うと、上記のboost/version.hppではなくboost/thread/thread.hppを使ってマルチスレッド処理を書こうとした所、
CMakeFiles\cpptest.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0': D:/boost/include/boost-1_59/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()' D:/boost/include/boost-1_59/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()' D:/boost/include/boost-1_59/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()' collect2.exe: error: ld returned 1 exit status CMakeFiles\cpptest.dir\build.make:96: recipe for target 'cpptest.exe' failed CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/cpptest.dir/all' failed mingw32-make.exe[2]: *** [cpptest.exe] Error 1 mingw32-make.exe[1]: *** [CMakeFiles/cpptest.dir/all] Error 2 mingw32-make.exe: *** [all] Error 2 Makefile:82: recipe for target 'all' failed
と出てしまい、上手く動かなかった。
どうやらgccのオプションで -lboost_systemを付けると良いらしいがよくわからない。
またこの辺は後日。
(それより早くOpenCVの記事書かなきゃ…)