3

Running xeus-cling(v0.13.0) under jupyter notebooks allows to run C++ code in code cells, sometimes.. Note: this is an issue with Jupyter notebook implementation of cling, Xeus -running cling at the command line does not have these problems.

For examples, It seems to randomly remember certain definitions and forget others:

In cell 1:

#include <iostream>
using std::cout;
using std::endl;

In cell 2:

cout << "D\n";
cout <<"D" << endl;

gives:

input_line_9:3:15: error: use of undeclared identifier 'endl'; did you mean 'std::endl'?
cout <<"D" << endl;
              ^~~~
              std::endl
/home/don/miniconda3/envs/xeus-cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:599:5: note: 'std::endl' declared here
    endl(basic_ostream<_CharT, _Traits>& __os)

Also, whether it will accept a function definition or not seems unpredictable to me. Here is an example:

In cell 1:

#include <iostream>

cell 2:

using namespace std;
void f2(){
    cout << "HI\n";
}

gives:

input_line_8:3:10: error: function definition is not allowed here
void f2(){
         ^
Interpreter Error: 

while, In cell 3:

using namespace std;
cout << "using std" << endl;

gives:

using std

then, in cells 4 & 5:

void f2(){
    cout << "HI, still using std.\n";
}
f2();

happily gives:

HI, still using std.

Is there some explanation out there for what xeus-cling is doing between cells?? how it's interpreting C++ (at a high, user level)? I do not see anything discussing this here readthedocs, or here.

More clues: In cell 1:

void a() {}
void b() {}

gives

error: function definition is not allowed here
 void b() {}

and seems can define at most one function per cell is an implicit rule of xeus-cling. Can we make these rules explicit?

Another bug:

struct A {
  A(int);
  int i;
};
A::A(int x) : i{x} {}

gives:

error: expected '{' or ','
A::A(int x) : i{x} void __cling_Un1Qu31(void* vpClingValue) {

but

A::A(int x) { i = x; }

is accepted. It's actually cling that can't accept class member initializer lists and xeus-cling inherits the bug.

But wrapping the above struct def followed by ctor def in a namespace results in no bug:

namespace aa {
  struct A {
    A(int);
    int i;
  };
}
namespace aa {
  A::A(int x) : i{x} {}
}
aa::A a{3};
a.i

prints out 3 in cling, and gives:

input_line_8:5:2: error: unknown type name 'a'
 a.i;

in xeus-cling, unless a.i is moved to a new cell where it will give 3.

Don Slowik
  • 965
  • 10
  • 18
  • "v0.13.0" <---- the "0" right after the "v" normally means "this is not a mature version, expect bugs, inconsistencies and random weirdness". – n. m. could be an AI Jan 20 '22 at 21:55
  • It looks pretty consistent to me: don't put 2 declarations in the same cell... Possibly the parser looks for a single declaration, and falls back to assuming this is a list of statements (that in normal C++ would need to be inside a function). – Marc Glisse Jan 20 '22 at 23:40
  • @n.1.8e9-where's-my-sharem. v0.13.0 is the most recent version listed at https://github.com/jupyter-xeus/xeus-cling and is what conda is using. What you point out may also explain things.. – Don Slowik Jan 21 '22 at 02:44
  • What version of C++ are you using? C++14 or C++17 or C++11? – Abdul ahad Jan 24 '22 at 06:58
  • @Abdulahad C++14. I had problems running the C++17 kernel. (common problem via google search) – Don Slowik Jan 24 '22 at 18:15
  • @MarcGlisse But try ```#include using namespace std; int i; int j; i = 3;``` in one cell, then ```cout << i;``` prints 3 in next cell, cout < – Don Slowik Jan 24 '22 at 18:18

1 Answers1

0

It looks like this is how the xeus-cling works like the explanation is given in this link:

Jupyter notebook error for C++ Kernel[cling]

Some rules are given in this link: https://code-ballads.net/generated-notebooks/cpp/repl_cling/notebooks/3_Advices_And_Gotchas.html

if cling repeatedly fails to compile/evaluate a cell (Function definition not allowed here, or various type errors), the kernel may have gotten corrupted / in a bad state, so just use Kernel -> Restart And Run All Cells. Actually the xeus-cling works as a interpreter that's why it reads only one instruction in a single cell and doesn't act like compiler.

Details are provided in this link: https://blog.jupyter.org/interactive-workflows-for-c-with-jupyter-fe9b54227d92

enter image description here

The difference between compiler and interpreter is given in this link: https://www.businessinsider.in/difference-between-compiler-and-interpreter/articleshow/69523408.cms#:~:text=Compliers%20and%20interpreters%20are%20programs,be%20understood%20by%20the%20computers.&text=Compiler%20scans%20the%20entire%20program,to%20analyze%20the%20source%20code. enter image description here

Abdul ahad
  • 1,383
  • 7
  • 18
  • xeus-cling can often handle more than one statement per cell... – Don Slowik Jan 24 '22 at 18:23
  • Yes, I'm familiar with the 'reboot' when kernel gets corrupted. It makes sense that, e.g., u can't redefine the same name multiple times (as in re-running same cell with a definition). And that the kernel itself is already 'running main()' so you don't want to define a 'main()' function. The second link you provided is a useful start to what's needed, but it should focus on the differences with C++ -we know we can't redefine things etc. But, Yes, a compendium of 'gotchas' and eventually a coherent understanding thereof. I'm managing to create C++ Jupyter notebooks and love it! (most of time) – Don Slowik Jan 25 '22 at 15:44