3

Is it possible to run C++ and Python code in the same Jupyter notebook?

I've taught myself R using Jupyter notebooks where I would first solve a coding problem in Python and then write a similar solution in R. Having the code blocks right below each other helped me keep my skills sharp in Python, while learning a new language at the same time.

I now want to teach myself C++ with the same approach.

Jake
  • 39
  • 1
  • 5
  • 6
    Not sure this is a good approach. One of the reasons you use C++ is to sit as close to the system hardware as possible, and that's not something you get to do in an interpreted universe. You will likely learn something C++-like, rather than C++. – user4581301 Aug 03 '21 at 23:53
  • In addition, "solve a coding problem" is one of the tougher approaches to picking up C++. I strongly recommend getting started with [a good set of books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and working through one of them lesson by lesson to pick up the fundamentals. C++ offers a lot of pathways to getting things done and some of them are a lot better than others. The sooner you get on a good path, the sooner you're producing efficient, easy to maintain code rather than code that simply works. – user4581301 Aug 03 '21 at 23:57

3 Answers3

2

Frankly, if you wanted to learn C++, I would encourage you to use an IDE with full-fledged support for C++ like Microsoft Visual Studio or Visual Studio Code, or Code::Blocks, among many others:

All of the above options support good compilers, debuggers and on-line C++ documentation (including Intellisense). You'd probably be doing a disservice to yourself if you chose an IDE that didn't have these features.

However, I believe you CAN run C++ (at least some versions, to some extent) from Jupyter:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

Xeus-Cling is your best bet as of this writing. Xeus is an implementation of the Jupyter protocol written in C++ for writing kernels. Xeus-Cling is a particular kernel, using Cling as an interpreter.

// in [1]
#include <iostream>
// in [2]
int square(int x) {return x*x;}
// in [3]
%%executable square.x -g
std::cout << square(4) <<std::endl;
// in [4]
!./square.x

Here %%executable square.x -g is a command to the linker on where to write an executable and -g is an option to enable debug information. ! tells it your passing a command, here ./ (./ aka source), which is how you would manually execute a program

If you are using linux it may be worth learning GDB as well to learn c++ debugging. That will require learning some basic x86 assembly. If you really want to dive into your executables (or perhaps someone elses) you can use Ghidra. You can learn these things via Computer Systems: A Programmer's Perspective. You can find classes online using the book, Harvard CS61, Carnegie Melon 15-213/15-513.

I would recommend learning these extra steps. The strength of systems languages is for low level systems programming and applications that need a high level of control and speed. So think physics engines, game engines, operating systems, embedded applications. It is not simply writing code, or even just knowing algorithms, it does require knowing what is going on underneath the hood. Otherwise its better to just stick with interpreted languages.

developer writeup for zeus, docs readthedocs, git github.

Revots
  • 27
  • 6
0

There are two ways to achieve that:

  1. Locally using xeus cling
  2. Online using binder

Locally:

xeus-cling is software that can achieve that but I don't think it is available for windows yet. I came across this some time back. Works fine on linux, you can run python and c++ code side by side on the same notebook
check out their documentation and github page for more:
https://xeus-cling.readthedocs.io/en/latest/index.html
https://github.com/jupyter-xeus/xeus-cling

Online/Web Browser:

Binder can also do the same online but I heard it is very slow and takes sometime to load. If you're fine with that you can use it.
their documentation page: https://mybinder.readthedocs.io/en/latest/

Avijit Dey
  • 21
  • 4