Visual Studio 2017 で Linux C++ 開発 (Boost C++ Libraryを使う)


準備

Boost C++ Library を利用するためインストールする必要がありますが、Linux 側だけでなく Windows 側にもインストールします。

ただし、Windows 側の Boost はビルドに使う訳ではなく、チェックに使われるだけなので、仮になくてもビルドはできてしまいます。

Ubuntu の場合はつぎのコマンドでインストールできます。(最新版が必要な場合は、Windows の場合と同じ場所からダウンロードして、独自にインストールします)

sudo apt install -y libboost-dev

Windows 側は次のサイトから Unix 用をダウンロードして、展開した後、適切なフォルダにコピーしておきます。

 http://www.boost.org/users/download/

プロジェクトの作成

プロジェクトの作成ですが、下記の投稿を参考にしてください。

 Visual Studio 2017 の Linux C++ 開発機能

Windows 側では Boost の場所をプロジェクトに教えてやる必要があります。プロジェクトのプロパティで Boost を保存したディレクトリをインクルードディレクトリに追加します。(参考 Fig.1)

Fig.1 インクルードパスの追加

サンプルプログラム

次のサンプルプログラムは、Boost の機能を利用して、文字列を指定した区切り文字で分割し、再び結合するものです。

List1. サンプルプログラム

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string/classification.hpp>

using namespace std;
using namespace boost;

//
//  Boost: 文字列の分割と結合のテスト
//  =================================
int main()
{
    const wchar_t* str0 = L"This is a pen.";
    vector<wstring> result;

    result = algorithm::split(result, str0, is_any_of(L" "));

    BOOST_FOREACH(wstring s, result)
    {
        wcout << s << endl;
    }

    wstring joined = algorithm::join(result, L",");

    wcout << joined << endl;

    // getchar();
    return 0;
}

プロジェクトを実行すると、最初の回だけ Linux への接続のためのダイアログが開きます。接続情報を入力するとビルドが行われ、エラーがなければプログラムが Linux 側で実行されます。もし、ブレークポイントを設定しておけば、そこで停止します。

Linux Console は「デバッグ」メニューの中にあるので、メニューをクリックして開きます。


Fig.2 Linux Console

-