You are here

Compiling macros

From within an interactive session (or inside a macro), it is possible to call compiled subroutines. This is what happens when invoking sin from the standard C library math.h, for example. When our macro is not trivial, it may take quite long time to execute it as interpreted code. In such case, it is possible to run a compiled version instead. Actually, this is a quite important option because, even if the compilation time is not negligible compared to the execution time, to build our macro we use a full C++ compiler so that we benefit from the complete set of checks that the compiler performs on our code (CINT is almost as powerful as the C++ compiler).

Our macro can be converted on-flight into a shared library by ACLiC, that is invoked by adding a plus sign (`+') to the name of our macro, when using either .x or the .L command. The compilation will fail with the macro given above, because we did not include the needed header files. After adding them:

root [] .!cat myMacro.cxx
#include <iostream>
#include <cmath>
using namespace std;
 
int myMacro(int k=0){
 
  cout << "The input parameter was " << k << endl;
  return k;
 
}
 
double anotherFunc(int j=0){
 
  double pp = 2*acos(-1);
  double x = 1.5*j + pp*myMacro(j);
  return x;
 
}
 
root [] .x myMacro.cxx+(-3)
Info in <TUnixSystem::ACLiC>: creating shared library /home/tmp/./myMacro_cxx.so
The input parameter was -3
(int)(-3)
root [] .L myMacro.cxx+
Info in <ACLiC>: unmodified script has already been compiled and loaded
root [] .L myMacro.cxx++
Info in <ACLiC>: unmodified script has already been compiled and loaded
Info in <ACLiC>: it will be regenerated and reloaded!
Info in <TUnixSystem::ACLiC>: creating shared library /home/tmp/./myMacro_cxx.so
root [] anotherFunc(2);
The input parameter was 2
(double)1.55663706143591725e+01
root [] .x myMacro.cxx++(3)
Info in <ACLiC>: unmodified script has already been compiled and loaded
Info in <ACLiC>: it will be regenerated and reloaded!
Info in <TUnixSystem::ACLiC>: creating shared library /home/tmp/./myMacro_cxx.so
The input parameter was 3
(int)3

As shown above, adding a single ` +' means "compile and load only if the code is new or has been modified". This is the preferred way to call ACLiC, because it saves time. For example, it is possible to have a loop in which we issue the first command, having ACLiC working only at the first iteration. If we want to force a new compilation, we have to add "++".