Xeus-Cling: Run C++ code in Jupyter Notebook

发布于 2023-12-22  276 次阅读


本文最后更新于2023年12月23日,已超过 60 天没有更新,如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

xeus-cling是一个Jupyter内核,用于C++编程语言。它基于Cling,一个用于C++的交互式解释器,由CERN的ROOT项目团队开发。
以下是关于xeus-cling项目的一些主要特性:

  1. 交互式C++:利用Jupyter Notebook的交互性,可以在Notebook中编写和执行C++代码,非常适合教育、数据分析和原型设计。
  2. Cling内核:Cling是一个C++解释器,提供了对现代C++的全面支持。因此,xeus-cling也支持C++11、C++14、C++17等现代C++标准。
  3. 易于集成:xeus-cling可以方便地与其他Jupyter生态系统中的工具和库集成,例如可以使用Jupyter的交互式小部件库。
  4. 开源:xeus-cling是一个开源项目,用户可以自由地使用、修改和分发。
    总的来说,xeus-cling提供了一种在Jupyter Notebook环境中使用C++的新方式,为数据科学家、研究人员、学生和教师提供了一个强大的工具。

目前只支持Linux 和 OS X平台,windows端可以使用docker或者wsl
作者建议用miniconda/miniforge开个独立环境使用。

To ensure that the installation works, it is preferable to install xeus-cling in a fresh environment. It is also needed to use a miniforge or miniconda installation because with the full anaconda you may have a conflict with the ZeroMQ library which is already installed in the anaconda distribution.
------引用自项目github文档[点击前往]

以下是我在wsl2 Ubuntu22.04.3LTS中的操作

  • conda版本为23.10.0,
  • python版本为3.9.18
  • mamba版本 22.9.0

安装miniconda

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh 
bash Miniconda3-latest-Linux-x86_64.sh

独立的环境

conda create -n cling python=3.9
conda activate cling

安装mamba

在项目的readme中作者以mamba安装为示例

conda install -c conda-forge mamba

安装notebook前端

mamba install jupyter notebook

安装xeus-cling

mamba install xeus-cling -c conda-forge

运行

运行jupyter即可,打开后会在kernel中可以看到c++11/14/17三个选项

jupyter notebook

也可以在vscode中使用,可以选择Existing Jupyter Server, 然后
选择Existing Jupyter Server
把wsl2中的jupyter notebook地址粘贴进去使用


有帮助的话请打个赏吧!