在 Python 中我们可以使用 Jupyter Notebook 直接看到结果, 例如:
l = [1,2]
l
直接输出:
[1,2]
那当使用 C++ 的时候, 例如:
- map<string, int> mp{
- {"one", 1},
- {"two", 2},
- {"three", 3},
- {"four", 4}
- };
如果要输出, 就得循环遍历, 可否直接输出结果呢?
so easy!!! Jupyter Notebook 可以解决一切问题, 哈哈~
看下图:
如何在 Jupyter 中玩 C++?
在 GitHub 上有一个仓库, 如下所示:
https://github.com/QuantStack/xeus-cling
xeus-cling 是一个用于 C++ 的 Jupyter 内核, 基于 C++ 解释器和 Jupyter 协议 xeus 的原生实现.
目前, 支持 Mac 与 Linux, 但不支持 Windows.
安装也是非常简单, 首先安装好 Anaconda, 在里面创建一个虚拟环境:
conda create -n cling
切换进去:
conda activate cling
给新环境安装 jupyter 和 notebook
conda install jupyter notebook
使用 conda-forge 安装 xeus-cling
conda install xeus-cling -c conda-forge
为了加速安装, 请记得给 Anaconda 配置源!
检查是否安装好了内核 (kernel):
jupyter kernelspec list
输出:
- python3 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/python3
- xcpp11 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/xcpp11
- xcpp14 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/xcpp14
- xcpp17 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/xcpp17
打开 Jupyter Notebook, 就可以看到看到 kernel 了.
启动 Jupyter Notebook:
- jupyter-notebook
- (其中多了一个 C, 是因为也装了 C kernel, 看后面)
示例测试:
如何在 Jupyter 中玩 C?
只需要安装 c kernel 即可!
可以直接在当前环境中创建 c kernel, 也可以新开一个环境安装, 下面是在当前环境中直接安装.
- pip install jupyter-c-kernel
- install_c_kernel
- jupyter kernelspec list
此时, 就输出:
- c /home/light/anaconda3/envs/cling/share/jupyter/kernels/c
- python3 /home/light/anaconda3/envs/cling/share/jupyter/kernels/python3
- xcpp11 /home/light/anaconda3/envs/cling/share/jupyter/kernels/xcpp11
- xcpp14 /home/light/anaconda3/envs/cling/share/jupyter/kernels/xcpp14
- xcpp17 /home/light/anaconda3/envs/cling/share/jupyter/kernels/xcpp17
启动 Jupyter Notebook:
jupyter-notebook
来源: https://www.qcloud.com/developer/article/1561252