# The correct usage of cling::Interpreter

**URL:** <https://root-forum.cern.ch/t/the-correct-usage-of-cling-interpreter/56191>\
**Category:** Cling\
**Tags:** cling\
**Created:** [August 29, 2023, 8:15am UTC](https://root-forum.cern.ch/t/the-correct-usage-of-cling-interpreter/56191 "2023-08-29T08:15:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![james\_white](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/james_white/32/34207_2.png) [@james\_white](https://root-forum.cern.ch/u/james_white)\
**Post date:** [August 29, 2023, 8:16am UTC](https://root-forum.cern.ch/t/the-correct-usage-of-cling-interpreter/56191/1 "2023-08-29T08:16:00Z")

</div>

My work consists of four CPP files, with two of them involving mutual function calls. In `dep.cpp`, the `onStart` function calls the `show` function in the `dog.cpp` file. When using cling::Interpreter to declare these CPP files successively (declare `dep.cpp` first and then declare `dog.cpp`), how can the issue of the `show()` function’s logic not being executed when the `onStart` function is executed be resolved?

For example:  
`dep.cpp` content:

```auto
#include "dog.h"
void Dep::onStart(){
    Dog dog;
    std::cout << "1b\n";
    dog.show();
    std::cout << "2c\n";
}

```

`dog.cpp` content:

```auto
void Dog::show(){
    std::cout << "dog show\n";
}

```

The result of executing this code is:

```auto
1b
2c

```

PS: I am unable to completely decouple the mutual calling relationship between the two CPP files.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [August 29, 2023, 9:10am UTC](https://root-forum.cern.ch/t/the-correct-usage-of-cling-interpreter/56191/2 "2023-08-29T09:10:01Z")

</div>

Dear @james_white ,

I don’t think this has really anything to do with ROOT or cling. Also, you don’t have any circular dependency in your logic. You are just missing some implementation details

### dep.cpp

```auto
#include <iostream>
#include "dep.hpp"
#include "dog.hpp"
void Dep::onStart(){
    Dog dog;
    std::cout << "1b\n";
    dog.show();
    std::cout << "2c\n";
}

```

### dep.hpp

```auto
#ifndef DEP
#define DEP

struct Dep{
	void onStart();
};

#endif // DEP

```

### dog.cpp

```auto
#include <iostream>
#include "dog.hpp"
void Dog::show(){
    std::cout << "dog show\n";
}

```

### dog.hpp

```auto
#ifndef DOG
#define DOG

struct Dog{
	void show();
};

#endif // DOG

```

### main.cpp

```auto
#include "dep.hpp"

int main(){
	Dep d;
	d.onStart();
}

```

Then you can just compile the main program with its dependencies and you’ll see the right output. Again, no ROOT and no interpreter here, just standard C++ code and some compiler knowledge.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![james\_white](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/james_white/32/34207_2.png) [@james\_white](https://root-forum.cern.ch/u/james_white)\
**Post date:** [September 1, 2023, 8:26am UTC](https://root-forum.cern.ch/t/the-correct-usage-of-cling-interpreter/56191/3 "2023-09-01T08:26:30Z")

</div>

Thank you very much for your reply.  
The problem I encountered is that there are interdependent functions in the input passed to the cling::Interpreter::declare(const std::string& input, Transaction\*\* T/\*=0 _/) function many times, resulting in abnormal running results .  
Later I switched to cling::Interpreter::parse(const std::string& input,  
Transaction_\* T = 0), the problem is solved, don’t care about the interdependence between inputs.

---

<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 15, 2023, 8:26am UTC](https://root-forum.cern.ch/t/the-correct-usage-of-cling-interpreter/56191/4 "2023-09-15T08:26:48Z")

</div>

Closed on September 15, 2023, 8:26am UTC

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