You are here

Mixing interpreted and compiled code

As we can call compiled code from within a macro or during an interactive session, we can also make use of the interpreter from within a compiled executable. Any command accepted by CINT during an interactive session can be issued from within a compiled program by using the method TROOT::ProcessLine(...) via the (pointer to the) global object gROOT, as shown below:

root [] .!cat myMacro.cxx
#include <iostream>
#include "TROOT.h"

using namespace std;

int myMacro(int k=0){

  gROOT->ProcessLine(".!ls"); // on Linux

  cout << "The input parameter was " << k << endl;
  return k;

}
root [] .x myMacro.cxx(3)
myMacro.cxx
The input parameter was 3
(int)3
root [] .x myMacro.cxx+(1)
Info in <aclic>: script has already been loaded in interpreted mode
Info in <aclic>: unloading /home/tmp/./myMacro.cxx and compiling it
Info in <tunixsystem::aclic>: creating shared library /home/tmp/./myMacro_cxx.so
myMacro.cxx  myMacro_cxx.so
The input parameter was 1
(int)3
root [] myMacro(6);
myMacro.cxx  myMacro_cxx.so
The input parameter was 6
(int)3

By using statements like the following ones, we can even compile and load another macro from a compiled program, inside another macro or during a ROOT interactive session!

gROOT->ProcessLine(".L myMacro.cxx++");
gROOT->ProcessLine("anotherFunc(3);");