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
:
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:
[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
:
#pragma cling add_library_path("/usr/lib/")
#pragma cling load( "libstdc++.dylib" )
#include <vector>
I also create a script test2.cpp
:
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:
[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!