Open6

VSCodeのJupyter Notebook拡張からC++を扱いたい。

fate_shelledfate_shelled
  1. C++のJupyterカーネルとしてclingをインストール
    https://github.com/root-project/cling
  • ここからパッケージをダウンロード

    私の場合、Ubuntu20.04の最新バージョンcling_2020-11-05_ROOT-ubuntu2004.tar.bz2をダウンロードしました。

  • 解凍
    好きな場所に展開してください。
    ここでは展開先/opt/clingとします。。

$ bzip2 -d cling_2020-11-05_ROOT-ubuntu2004.tar.bz2
$ sudo mkdir /opt/cling
$ tar xvf cling_2020-11-05_ROOT-ubuntu2004.tar -C /opt/cling
fate_shelledfate_shelled
  1. Jupyterから使用できるように設定する。
$ cd /opt/cling/cling_2020-11-05_ROOT-ubuntu2004/share/cling/Jupyter/kernel
$ sudo pip3 install -e .
$ jupyter-kernelspec install --user cling-cpp11
$ jupyter-kernelspec install --user cling-cpp14
$ jupyter-kernelspec install --user cling-cpp17
fate_shelledfate_shelled
  1. VSCodeの設定

環境変数'PATH'にclingのbinディレクトリを指定します。
VSCodeのsetting.jsonに下記項目を追加します。

{
    "terminal.integrated.env.linux": {
        "PATH": "${env:PATH}:/opt/cling/cling_2020-11-05_ROOT-ubuntu2004/bin"
    },    
}

VSCodeを再起動します。
VSCodeを再起動してもうまく動作しない場合、PCごと再起動しました。

fate_shelledfate_shelled
  1. 動作させてみる。
  • 好きなディレクトリでVSCodeを開きます。
  • ノートブックを作成します。ここではcpp.ipynbとします。
  • ノートブックを開き、カーネルでC++を選択。

下記コードをセルに入力して実行。

#include <iostream>
using namespace std;

std::cout << "hello world!" << std::endl;