If you want to interactively write C and C++ in Jupyter Notebook, this article is for you

1. Preparation

In order to reduce unnecessary trouble in the process of installation, affect the mood of you beautiful, please make sure that on the target machine has installed the Anaconda, download address: www.anaconda.com/distributio… . If yes, skip this step.

2. Installation environment

The environments listed below are those that have been tested in practice and are for reference only, not strictly consistent.

The 2.1 Linux

  • CentOS Linux release 7.4.1708 (Core)
  • Anaconda3
  • Conda 4.6.11

2.2 macOS

  • MacOS Mojave 10.14.4
  • Anaconda3
  • Conda 4.6.14

Note: Windows is not currently well supported

3. Start the installation

C language and C++ are supported by different kernels. There is no dependency between the two, so you can install only one or both according to your needs. However, it is recommended to install the C++ kernel, which is the mainstream kernel and is maintained by a more professional team. The implementation is simple, but if you want a quick experience running C in Jupyter Notebook, it’s a good choice. For more information about the kernel supported by Jupyter, see github.com/jupyter/jup…

3.1 installing C++ kernel (xeus-cling)


  • Create a new virtual environment namedcling, or whatever you prefer, for example, if you want to install C++ and C Kernel in this environmentc_cpp
conda create -n cling
Copy the code
  • Switch to the newly created virtual environment
conda activate cling
Copy the code
  • Install the new environmentjupyterandnotebook
conda install jupyter notebook
Copy the code
  • useconda-forgeMirroring Channel Installationxeus-cling
conda install xeus-cling -c conda-forge
Copy the code
  • Check whether the kernel is installed successfully
jupyter kernelspec list
Copy the code

If installed correctly, the following four kernels will be displayed:

python3 /anaconda3/envs/cling/share/jupyter/kernels/python3

xcpp11 /anaconda3/envs/cling/share/jupyter/kernels/xcpp11

xcpp14 /anaconda3/envs/cling/share/jupyter/kernels/xcpp14

xcpp17 /anaconda3/envs/cling/share/jupyter/kernels/xcpp17

  • Open the notebook
jupyter notebook
Copy the code

In the new drop-down menu, you can see the above four kernels and select C++ 11:

Copy and paste the following c++ code into the cell, press shift+enter, and run the c++ code. Enjoy it! :

#include <iostream>

std: :cout << "Hello world!" << std: :endl;
Copy the code

Added: Quick installation

If you don’t want to take it all in one step, or if you’re a hothead like me and want to take it all in one step, here’s how to do it:

  • Create a new file and name itcling.yml, copy and paste the following contents intocling.ymlIn:
name: cling
channels:
    - conda-forge
    - https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
    - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    - defaults
dependencies:
    - python=3
    - PIP = 19.2.1
    - jupyter
    - notebook
    - Xeus - cling = 0.7.1
Copy the code
  • Create virtual environments from YML files
conda env create -f cling.yml
Copy the code

This method is recommended for the second installation of the same environment, essentially exporting the above environment. If it is the first installation, it is recommended to try the step-by-step installation method to deepen the understanding of the operating mechanism of the kernel, so that you can install the kernel of other languages in the future.

3.2 Installing C Kernel (JUPyter-C-kernel)

Jupyter-c-kernel is the best supported C kernel provided by the third party officially listed at present, which is a good choice before there is a better C kernel. If you are familiar with the c ++ Kernel installation, then the installation of Jupyter-c-kernel is similar, so instead of enumerating the steps, we put all the installation commands together:

3.2.1 Creating a Virtual Environment

If you want to install the C kernel separately in a new environment, you can use the following commands (you can copy the whole thing into a shell script, run it from the command line, or run it line by line) :

conda create -n clang
conda activate clang
conda install jupyter notebook
pip install jupyter-c-kernel
install_c_kernel
jupyter kernelspec list
jupyter notebook
Copy the code

3.2.2 Installation on an Existing VIRTUAL Environment

If you do not want to repeat the installation of JUPyter, you can install JUPyter-C-kernel directly with PIP in the previous environment (cling or C_CPP) :

pip install jupyter-c-kernel
Copy the code

It should be noted that unlike XEUS-cling which can be seen by using the command Jupyter Kernelspec list after installing JUPyter-C-kernel with PIP, The command install_c_kernel will be generated in the path of the current environment executable program. You need to run the command install_c_kernel separately. Of course, it is automatically added to the environment variable, so you can run it directly. (For those of you who are careful, you will notice that this command was also run in the previous section.)

install_c_kernel
# Check the installed kernel
jupyter kernelspec list
Copy the code

Open the Jupyter Notebook again as follows:

Select C, create a new notebook, and copy the following C code to the cell:

#include <stdio.h>

int main (a) {
    printf("Hello world! \n");
    return 0;
}
Copy the code

Run C code and enjoy it! :

summary

If you want to install two kernels at a time using the ‘yML’ file in 3.1, just make minor changes to the 3.1 file as follows:

name: c_cpp
channels:
    - conda-forge
    - https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
    - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    - defaults
dependencies:
    - python=3
    - PIP = 19.2.1
    - jupyter
    - notebook
    - Xeus - cling = 0.7.1
    - pip:
        - Jupyter - c - kernel = = 1.2.2
Copy the code

Copy and paste to the file c_cpp.yml and run the following command in the same directory as c_cpp.yml:

conda env create -f c_cpp.yml
conda activate c_cpp
install_c_kernel
jupyter kernelspec list
Copy the code

reference

  • [1] Jupyter-kernels jupyter Github
  • [2] xeus-cling QuantStack Github
  • [3] jupyter-c-kernel brendan-rius Github

Writing a column is not easy, so if you find this article helpful, give it a thumbs up. Thanks for your support!

  • Personal website: Kenblog.top
  • Github site: kenblikylee.github. IO



Wechat scan qr code to obtain the latest technology original