# Unable to re-run same script twice in cling

**URL:** <https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572>\
**Category:** Cling\
**Tags:** cling\
**Created:** [August 30, 2021, 5:53pm UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572 "2021-08-30T17:53:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kmcrane](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kmcrane/32/27243_2.png) [@kmcrane](https://root-forum.cern.ch/u/kmcrane)\
**Post date:** [August 30, 2021, 5:53pm UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/1 "2021-08-30T17:53:11Z")

</div>

I posted this issue in the cling GitHub, but since I’m new to cling I’m not sure if it’s genuinely a bug—or just a misunderstanding of how cling works/is used. The GitHub issue is issue 428 in root-project/cling/issues/ But since I’m a new forum user I can’t add a link to this issue directly, and have re-described it below.

**In short:** as a warm-up, I wrote a little C++ script in cling. Executing it once works exactly as expected (amazing!) but running it a second time yields all sorts of errors, telling me that basic things (such as the constructor for a std::vector) are no longer defined. This happens even if I unload the script with “.U” before the second invocation. I’ve also had this same problem when trying to use cling with other libraries beyond libstdc++, which is making it hard to use cling in the way I had hoped.

**In more detail:**

Here’s an example that does **not** involve data structures from any library, and works fine: I can run this script many times, and even change the constant values between invocations. Suppose in particular that we define the file `test1.cpp`:

```auto
int test1() {
   int a = 9;
   int b = 12;
   int sum = a+b;

   return sum;
}

```

Running `.x test1.cpp` twice from within cling produces the expected result—the script runs twice, producing the same output each time:

```auto
[cling]$ .x test1.cpp
(int) 21
[cling]$ .x test1.cpp
(int) 21

```

Next, consider an example that uses a `std::vector`. First, in order to use this data structure, I create a file `test2.h`:

```auto
#pragma cling add_library_path("/usr/lib/")
#pragma cling load( "libstdc++.dylib" )

#include <vector>

```

I also create a script `test2.cpp`:

```auto
int test2() {
   std::vector<int> x( 10 );

   for( int i = 0; i < 10; i++ )
      x[i] = 1;

   int sum = 0;
   for( int i = 0; i < 10; i++ )
      sum += x[i];

   return sum;
}

```

I now run `cling -l test2.h`, followed by two invocations of the script `test2.cpp`. (I also tried running `.U test.cpp` in between the two invocations, which yields the same behavior.) The first invocation returns the expected result (10), but on the second invocation cling complains that the definition of the default constructor is now missing. Here is the output:

```auto
[cling]$ .x test2.cpp
(int) 10
[cling]$ .x test2.cpp
In file included from input_line_3:1:
In file included from ./test2.h:4:
/usr/local/opt/llvm/bin/../include/c++/v1/vector:362:5: error: constructor for 'std:: __1::__ vector_base<int, std::__1::allocator<int> >' must explicitly initialize the base class
      '__vector_base_common<true>' which does not have a default constructor
    __vector_base()
    ^
/usr/local/opt/llvm/bin/../include/c++/v1/vector:515:14: note: in instantiation of member function 'std:: __1::__ vector_base<int, std:: __1::allocator<int> >::__ vector_base' requested here
    explicit vector(size_type __n);
             ^
/Users/kmcrane/test2.cpp:2:21: note: in instantiation of member function 'std:: __1::vector<int, std::__ 1::allocator<int> >::vector' requested here
   std::vector<int> x( 10 );
                    ^
/usr/local/opt/llvm/bin/../include/c++/v1/vector:324:64: note: 'std:: __1::__ vector_base_common<true>' declared here
_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
                                                               ^
In file included from input_line_3:1:
In file included from ./test2.h:4:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/vector:276:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/__bit_reference:15:
/usr/local/opt/llvm/bin/../include/c++/v1/algorithm:2587:12: error: type 'std:: __1::__ less<unsigned long, unsigned long>' does not provide a call operator
    return __comp(__b, __a) ?__b : __a;
           ^ ~~~~~
/usr/local/opt/llvm/bin/../include/c++/v1/algorithm:2596:19: note: in instantiation of function template specialization 'std:: __1::min<unsigned long, std::__ 1::__less<unsigned long, unsigned
      long> >' requested here
    return _VSTD::min( __a,__ b, __less<_Tp>());
                  ^
/usr/local/opt/llvm/bin/../include/c++/v1/vector:1014:19: note: in instantiation of function template specialization 'std::__1::min<unsigned long>' requested here
    return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
                  ^
/usr/local/opt/llvm/bin/../include/c++/v1/vector:991:15: note: in instantiation of member function 'std:: __1::vector<int, std::__ 1::allocator<int> >::max_size' requested here
    if (__n > max_size())
              ^
/usr/local/opt/llvm/bin/../include/c++/v1/vector:1126:9: note: in instantiation of member function 'std:: __1::vector<int, std::__ 1::allocator<int> >::__vallocate' requested here
        __vallocate(__n);
        ^
/Users/kmcrane/test2.cpp:2:21: note: in instantiation of member function 'std:: __1::vector<int, std::__ 1::allocator<int> >::vector' requested here
   std::vector<int> x( 10 );

```

**Here’s my setup:**

cling version: 0.9  
MacOS version 10.15.17  
installed via Homebrew on August 28, 2021

Many thanks for taking a look!

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 30, 2021, 6:17pm UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/2 "2021-08-30T18:17:45Z")

</div>

Probably not related, but there are some pending issues with reloading things in cling:  
[https://sft.its.cern.ch/jira/browse/ROOT-8689](https://sft.its.cern.ch/jira/browse/ROOT-8689)

---

<div class="post-metadata">

**Author:** ![kmcrane](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kmcrane/32/27243_2.png) [@kmcrane](https://root-forum.cern.ch/u/kmcrane)\
**Post date:** [August 30, 2021, 7:14pm UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/3 "2021-08-30T19:14:10Z")

</div>

Thanks! Yeah, not clear it’s directly related but good to know.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 31, 2021, 6:22am UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/4 "2021-08-31T06:22:35Z")

</div>

This JIra issue is from @Axel . May be he can give your more details.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [August 31, 2021, 6:35am UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/5 "2021-08-31T06:35:57Z")

</div>

Totally related, this is one of the known issues we have and need to address when reloading templates.

Is this blocking your actual use case?

I tried to allow links to [github.com](http://github.com) even from new users - are you telling me this doesn’t work?

---

<div class="post-metadata">

**Author:** ![kmcrane](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kmcrane/32/27243_2.png) [@kmcrane](https://root-forum.cern.ch/u/kmcrane)\
**Post date:** [August 31, 2021, 10:18am UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/6 "2021-08-31T10:18:05Z")

</div>

> Is this blocking your actual use case?

Yes, though I have to confess that our usage is not related to ROOT. We are using cling to build an interactive console on top of the GeometryCentral and Polyscope libraries for polygon mesh processing/visualization. (I unfortunately can’t add links to these libraries, but if you Google them they will turn up at the top of the search). So far things work ok, but when we try to use some standard features in a script, it becomes impossible to “edit and re-run”, which is a common usage pattern for people used to working in, say, a MATLAB-like interactive environment.

For instance, we use range-based for loops which are defined in terms of templates, which seems to cause trouble for reloading. That’s why I constructed the minimal example above of using range-based for on a std vector, but in the end it sounds like it may be templates and not range-based loops that are the issue (in fact, I can confirm this with another example where I define a custom type with range-based loops inline—which works fine upon multiple invocations).

> I tried to allow links to github even from new users - are you telling me this doesn’t work?

Correct. The forum simply prevents new users from posting links to any site (including GitHub).

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [August 31, 2021, 1:07pm UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/7 "2021-08-31T13:07:14Z")

</div>

> [@kmcrane](#):
>
> Yes, though I have to confess that our usage is not related to ROOT.

There’s nothing worth confessing here. We need to fix this, but we needed to fix this since a while, and so far we didn’t manage to allocate the expert time we need for this. Do you have clang-expert volunteers on your side? (Just asking 🙂 )

Thanks for the info on posting links; I’ll investigate…

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 31, 2021, 2:03pm UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/8 "2021-08-31T14:03:00Z")

</div>

Does your code work if you remove `#include <vector>` (but still use vectors)? That could be a workaround. I saw that behaviour on ROOT 5, which needed this include but didn’t let you re-run the macro; at some point (probably since ROOT 6) this include became unnecessary at least when running from the interactive prompt. And you may also need to clear the vectors at the end of your macro to avoid problems on subsequent runs.

---

<div class="post-metadata">

**Author:** ![kmcrane](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kmcrane/32/27243_2.png) [@kmcrane](https://root-forum.cern.ch/u/kmcrane)\
**Post date:** [August 31, 2021, 2:48pm UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/9 "2021-08-31T14:48:32Z")

</div>

> [@Axel](#):
>
> Do you have clang-expert volunteers on your side? (Just asking 🙂 )

There are certainly some folks at CMU who know clang/LLVM well… but not that I know (well) personally!

Thanks for taking a look, and let me know if I can do anything else to help.

---

<div class="post-metadata">

**Author:** ![kmcrane](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kmcrane/32/27243_2.png) [@kmcrane](https://root-forum.cern.ch/u/kmcrane)\
**Post date:** [August 31, 2021, 2:50pm UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/10 "2021-08-31T14:50:01Z")

</div>

> [@dastudillo](#):
>
> Does your code work if you remove `#include <vector>` (but still use vectors)?

No, unfortunately I then just get the usual “implicit instantiation of undefined template.”

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 14, 2021, 2:50pm UTC](https://root-forum.cern.ch/t/unable-to-re-run-same-script-twice-in-cling/46572/11 "2021-09-14T14:50:56Z")

</div>

Closed on September 14, 2021, 2:50pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
