若是你想在jupyter notebook中交互式編寫C以及C++,那麼本文是爲你而寫^_^python
爲了減小安裝過程當中沒必要要的煩惱,影響你美美的心情,請確保目標機器上已經安裝了Anaconda,下載地址:www.anaconda.com/distributio…。如已安裝,請跳過。ios
如下列舉環境是通過實踐檢驗的環境,僅供參考,並不是要求嚴格一致。c++
注:windows目前還未獲得較好支持git
C語言和C++由不一樣kernel支持,二者沒有依賴關係,所以能夠根據須要只安裝其中一個,或兩個都安裝,但推薦安裝C++ kernel,這是主流的kernel,由更加專業的團隊維護的項目,C kernel是由我的開發者維護的小型項目,實現較爲簡單,但若是想快速體驗在jupyter notebook運行C,也是不錯的選擇。若是想了解更多jupyter支持的kernel,能夠參考github.com/jupyter/jup…github
cling
,或者你喜歡的其餘名稱,好比,若是你想在此環境安裝C++和C Kernel,能夠取名爲c_cpp
conda create -n cling
複製代碼
conda activate cling
複製代碼
jupyter
和notebook
conda install jupyter notebook
複製代碼
conda-forge
鏡像channel安裝xeus-cling
conda install xeus-cling -c conda-forge
複製代碼
jupyter kernelspec list
複製代碼
正確安裝,會顯示如下四個kernel:shell
python3
/anaconda3/envs/cling/share/jupyter/kernels/python3windows
xcpp11
/anaconda3/envs/cling/share/jupyter/kernels/xcpp11微信
xcpp14
/anaconda3/envs/cling/share/jupyter/kernels/xcpp14網站
xcpp17
/anaconda3/envs/cling/share/jupyter/kernels/xcpp17ui
jupyter notebook
複製代碼
在新建下拉菜單裏能夠看到看到上面的四個kernel,選擇C++ 11
:
將如下c++代碼複製黏貼到cell中,按下shift+enter
,運行C++代碼,enjoy it!:
#include <iostream>
std::cout << "Hello world!" << std::endl;
複製代碼
若是你不想一步步看完,或者你和我同樣是個急性子的人,想一步到位,那麼可使用如下方法,一步搞定:
cling.yml
, 將如下內容複製黏貼到cling.yml
中: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
複製代碼
conda env create -f cling.yml
複製代碼
沒錯只需兩步,你已經完成和前面等效的環境建立,此方法推薦用於二次安裝相同環境,本質是導出了上述環境,若是是首次安裝,建議嘗試一步步來的安裝方式,更能加深對kernel工做機制的理解,以便之後安裝其餘語言的kernel。
目前官方列舉的第三方提供的C kernel,支持的比較好的是jupyter-c-kernel
,在沒有更好的C kernel出來前,這是一個不錯選擇。若是你熟悉了C++ Kernel的安裝,那麼安裝jupyter-c-kernel
也是大同小異,所以就不一一列舉步驟,而是將全部安裝命令彙總在一塊兒:
若是你想單獨在一個新的環境安裝C kernel, 可使用以下命令(可將所有複製到一個shell
腳本中,在命令行運行, 或一行行運行):
conda create -n clang
conda activate clang
conda install jupyter notebook
pip install jupyter-c-kernel
install_c_kernel
jupyter kernelspec list
jupyter notebook
複製代碼
若是你不想重複安裝jupyter, 能夠在前面的環境裏(cling
或c_cpp
),直接使用pip安裝jupyter-c-kernel
:
pip install jupyter-c-kernel
複製代碼
須要注意的是,不一樣於xeus-cling
在使用conda安裝後,就能夠經過命令jupyter kernelspec list
查看到,使用pip安裝jupyter-c-kernel
後,會在當前環境可執行程序路徑生成一個可運行命令install_c_kernel
,須要再單獨運行此命令install_c_kernel
, 固然通常都已自動添加到環境變量裏,所以能夠直接運行。(細心的同窗,應該注意到在上一小節也運行了這個命令)
install_c_kernel
# 查看已安裝kernel
jupyter kernelspec list
複製代碼
再次打開jupyter notebook
, 以下:
選擇C
,新建notebook
, 將如下C代碼複製到cell:
#include <stdio.h>
int main () {
printf("Hello world!\n");
return 0;
}
複製代碼
運行C代碼, enjoy it!:
若是你想像3.1中使用‘yml’文件一次安裝兩個kernel,只要在3.1的文件上基礎稍加改動,以下:
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
複製代碼
複製黏貼到文件c_cpp.yml
, 並在c_cpp.yml
相同目錄下運行以下命令:
conda env create -f c_cpp.yml
conda activate c_cpp
install_c_kernel
jupyter kernelspec list
複製代碼
堅持寫專欄不易,若是以爲本文對你有幫助,記得點個贊。感謝支持!
微信掃描二維碼 獲取最新技術原創