Using TTimer in standalone ROOT application

I am trying to use TTimer and want to call a simple function every second.
I am using the following code, which you can see in the attachment.

from ROOT prompt i am able to run the code and it works expectedly

but when i am running it as standalone executable then i am getting following messages
during run time

[color=#FF0000]“input_line_29:2:3: error: use of undeclared identifier ‘Update2’
(Update2())”[/color]

where Update2() is the function that i want to call everysecond.

for standalone mode i am compiling like this

[color=#408040]g++ -o testTimer testTimer.cpp root-config --cflags --libs[/color]

I want standalone executable to run as it is running at ROOT prompt
Please suggest me how to resolve this issue.

Thanks and with Regards,
Raman
testTimer.cpp (423 Bytes)

Hi Raman,

this is a subtelty between what code is interpreted and what is compiled.
If you want to invoke the function from the TTimer, the interpreter must be aware of it. For this reason it is necessary to “inject it” in the interpreter as string and not only compile it.
So, concretely starting from your example:

#include "TTimer.h"
#include <iostream>
#include "TInterpreter.h"
#include "TApplication.h"

void testTimer(){
   TTimer *timer = new TTimer(1000);
   timer->SetCommand("Update2()");
   timer->TurnOn();
}


int main(){
   const char* code = "int evNo = 0;"
                      "void Update2(){"
                      "evNo++;"
                      "std::cout<<\"Ev No : \"<< evNo << std::endl;"
                      "}";
   gInterpreter->Declare(code);
   TApplication *fApp = new TApplication("Test", NULL, NULL);
   testTimer();
   fApp->Run();
   return 0;
}

Cheers,
Danilo

Thanks Danilo,
Your solution worked

cheers,
Raman

Hi Raman,

I was thinking about my previous answer.
Actually, you could leverage the fact that the function code is in memory already and just inject the function prototype:

#include "TTimer.h"
#include <iostream>
#include "TInterpreter.h"
#include "TApplication.h"

void testTimer(){
   TTimer *timer = new TTimer(1000);
   timer->SetCommand("Update2()");
   timer->TurnOn();
}

int evNo = 0;
void Update2(){
evNo++;
std::cout<<"Ev No : "<< evNo << std::endl;
}


int main(){
   const char* code = "void Update2();";
   gInterpreter->Declare(code);
   gInterpreter->ProcessLine("Update2()");
   TApplication *fApp = new TApplication("Test", NULL, NULL);
   testTimer();
   fApp->Run();
   sleep(10);
   return 0;
}

But that’s a second order improvement.

D