The correct usage of cling::Interpreter

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:

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

dog.cpp content:

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

The result of executing this code is:

1b
2c

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

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

#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

#ifndef DEP
#define DEP

struct Dep{
	void onStart();
};

#endif // DEP

dog.cpp

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

dog.hpp

#ifndef DOG
#define DOG

struct Dog{
	void show();
};

#endif // DOG

main.cpp

#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

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.

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