3

在 jupyter notebooks 下运行 xeus-cling(v0.13.0) 允许在代码单元中运行 C++ 代码,有时.. 注意:这是 Jupyter notebook 实现 cling 的问题,Xeus -running cling 在命令行没有这些问题.

例如,它似乎会随机记住某些定义而忘记其他定义:

在单元格 1 中:

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

在单元格 2 中:

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

给出:

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)

此外,它是否会接受函数定义对我来说似乎是不可预测的。这是一个例子:

在单元格 1 中:

#include <iostream>

单元格 2:

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

给出:

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

同时,在单元格 3 中:

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

给出:

using std

然后,在单元格 4 和 5 中:

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

高兴地给出:

HI, still using std.

是否有一些解释 xeus-cling 在细胞之间的作用?它如何解释 C++(在高级用户级别)?我在此处阅读文档此处没有看到任何讨论此内容的内容。

更多线索:在单元格 1 中:

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

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

并且似乎每个单元最多可以定义一个函数是 xeus-cling 的隐含规则。我们可以明确这些规则吗?

另一个错误:

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

给出:

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

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

被接受。它实际上是 cling 不能接受类成员初始化器列表,而 xeus-cling 继承了这个 bug。

但是将上述 struct def 后跟 ctor def 包装在命名空间中不会导致错误:

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

3在紧贴中打印出来,并给出:

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

在 xeus-cling 中,除非a.i被移动到一个新的单元格,它将给出3.

4

1 回答 1

0

看起来这就是 xeus-cling 的工作方式,就像此链接中给出的解释一样:

C++ 内核的 Jupyter 笔记本错误 [cling]

此链接中给出了一些规则: https ://code-ballads.net/generated-notebooks/cpp/repl_cling/notebooks/3_Advices_And_Gotchas.html

如果 cling 反复无法编译/评估单元格(此处不允许函数定义,或各种类型错误),内核可能已损坏/处于错误状态,因此只需使用内核 -> 重新启动并运行所有单元格。实际上,xeus-cling 作为解释器工作,这就是为什么它在单个单元格中只读取一条指令并且不像编译器那样工作。

此链接中提供了详细信息: https ://blog.jupyter.org/interactive-workflows-for-c-with-jupyter-fe9b54227d92

在此处输入图像描述

这个链接给出了编译器和解释器的区别: https ://www.businessinsider.in/difference-between-compiler-and-interpreter/articleshow/69523408.cms#:~:text=Compliers%20and%20interpreters%20are %20 个程序,被%20%20%20%20%20 计算机理解。&text=编译器%20 扫描%20%20 整个%20 程序,以%20 分析%20%20 源%20 代码在此处输入图像描述

于 2022-01-24T08:01:58.717 回答