Can't open macros with ROOT

Hi everyone

I just installed ROOT 6.24/02 for Mac OS BigSur v11.4. There are no error messages when I launch ROOT, but when I try to open any macro I receive the next error message:

Processing main.cpp…
Error in TApplication::TApplication: only one instance of TApplication allowed

You know what could I do to open my macros?

Regards

It seems your macro has a main() ? can you post a none working macro here ?

Thank you. Indeed it has a main(), here’s my macro:

#include <iostream>
using namespace std;

int main(){
int i;
int fact=1;
int numero;

cout << "ingresa un numero"<<endl;
cin>>numero;

if (numero<0) {fact =0;}
else if (numero==0) {fact=1;}
else {
for (i = 1; i <= numero; i++){
fact = fact*i;
}
}
cout<<" El Factorial de "<< numero<< " es: " << fact << endl;

return 0;
}

factorial.C:

void factorial(){
   int i;
   int fact=1;
   int numero;

   cout << "ingresa un numero"<<endl;
   cin>>numero;

   if (numero<0) {fact =0;}
   else if (numero==0) {fact=1;}
   else {
      for (i = 1; i <= numero; i++){
         fact = fact*i;
      }
   }
   cout<<" El Factorial de "<< numero<< " es: " << fact << endl;
}

Execution:

root [0] .x factorial.C
ingresa un numero
3
 El Factorial de 3 es: 6
root [1] 
1 Like

Hi @Leonardo ,
and welcome to the ROOT forum.
To expand on Olivier’s answer: you can write ROOT programs as standard C++ programs (i.e. with a main function) and compile them as usual (e.g. with g++ -o program program.cpp $(root-config --libs --cflags)) or you can write what we usually call a “macro” or a script, in which case when you run it with root macro.C or, from the ROOT prompt, as [0] .x macro.C, ROOT executes the function in that file that has the same name as the file (i.e. it would try to execute the function macro() when you do root macro.C).

Executing a macro with a main function in it seems to confuse the ROOT C++ interpreter.

I hope this clarifies things a bit.

Cheers,
Enrico

1 Like

Thank you Enrico, that helped a lot!

This chapter in the ROOT Manual should cover this question.