Interpretation and Compilation

As you observed, up to now we heavily exploited the capabilities of ROOT for interpreting our code, more than compiling and then executing. This is sufficient for a wide range of applications, but you might have already asked yourself "how can this code be compiled ?". There are two answers.

Compile a Macro with ACLiC[edit | edit source]

ACLiC will create for you a compiled dynamic library for your macro, without any effort from your side, except the insertion of the appropriate header files in lines 5--11. In this example, they are already included. To generate an object library from the macro code, from inside the interpreter type (please note the "+"):

 root [1] .L macro1.C+

Once this operation is accomplished, the macro symbols will be available in memory and you will be able to execute it simply by calling from inside the interpreter:

 root [2] macro1()

Compile a Macro with the Compiler[edit | edit source]

A plethora of excellent compilers are available, both free and commercial. We will refer to the GCC compiler in the following. In this case, you have to include the appropriate headers in the code and then exploit the root-config tool for the automatic settings of all the compiler flags. root-config is a script that comes with ROOT; it prints all flags and libraries needed to compile code and link it with the ROOT libraries. In order to make the code executable stand-alone, an entry point for the operating system is needed, in C++ this is the procedure int main();. The easiest way to turn a ROOT macro code into a stand-alone application is to add the following "dressing code" at the end of the macro file. This defines the procedure main, the only purpose of which is to call your macro:

int main() {
  ExampleMacro();
  return 0;
}

To create a stand-alone program from a macro called ExampleMacro.C, simply type

 > g++ -o ExampleMacro ExampleMacro.C `root-config --cflags --libs`

and execute it by typing

> ./ExampleMacro

This procedure will, however, not give access to the ROOT graphics, as neither control of mouse or keyboard events nor access to the graphics windows of ROOT is available. If you want your stand-alone application have display graphics output and respond to mouse and keyboard, a slightly more complex piece of code can be used. In the example below, a macro ExampleMacro_GUI is executed by the ROOT class TApplication. As a additional feature, this code example offers access to parameters eventually passed to the program when started from the command line. Here is the code fragment:

void StandaloneApplication(int argc, char** argv) {
  // eventually, evaluate the application parameters argc, argv
  // ==>> here the ROOT macro is called
  ExampleMacro_GUI();
}
  // This is the standard "main" of C++ starting
  // a ROOT application
int main(int argc, char** argv) {
   TApplication app("ROOT Application", &argc, argv);
   StandaloneApplication(app.Argc(), app.Argv());
   app.Run();
   return 0;
}

Compile the code with

 > g++ -o ExampleMacro_GUI ExampleMacro_GUI `root-config --cflags --libs`

and execute the program with

> ./ExampleMacro_GUI