Compiling several source (main and headers) files and linking them?

Suppose I have a main macro named “macro.cpp” and two headers “h1.cpp” (contains the definition of a function) and “h1.h” containing the declaration of the function defined in “h1.cpp” similarly I have “h2.cpp” and “h2.h”. The main program “macro.cpp” calls those functions inside “h1” and “h2”. I was successful compiling the source files using:

.L h1.cpp+
.L h2.cpp+
.L macro.cpp+

which generated three .so files “, and”. I want to know what to do with them ? How do I link them so that I have something like a “macro.out” or something like that (a single executable file) which I can execute (I don’t know how !) and do whatever I wished to do with the macro.

Note: If I just load all the files using .L file_name.cpp etc and just execute the main macro using .x macro.cpp then everything works fine and I have results, but this is not what I want ! I want to compile like we do in usual g++ and by the way in every forum everyone keep advising on compiling using .L file_name.cpp+ or ++ … I would really like to know the whole story… because nobody seems to explain beyond .L file_name.cpp+ … what next ? What to do with the .so etc.

Thanks a lot


first of all, before proceeding further, I would suggest to go through the ROOT primer, in particular the chapter about macros: … oot-macros

Then, about your post I could identify several questions, I try to address them.

You can always obtain an executable compiling your code, that is independent from ROOT. For rapid prototyping, ROOT can be of great help: you can run your macro with something like

root myMacro.C

where a function called myMacro must be contained in the file.

When you have a shared library, you load it. You can do it for example with the TSystem::Load (gSystem->Load(“”)). Once it it in memory, you can call symbols, e.g. functions, within it thanks to the ROOT interpreter. If you run

root myMacro.C+

twice, the first time the macro is compiled into a shared library, the library is loaded and the function called myMacro executed. If you do it more than once without modifying the code of myMacro.C, ROOT will not re-compile the macro but just load and execute. You can force recompilation with an additional “+” at the end of the line (using .L and .L myMacro.C+ at the prompt is equivalent to the command lines we discussed above).

I hope this help.


Create a simple “trial.cxx” file (an “unnamed” ROOT macro): { gROOT->LoadMacro("h1.cpp+"); gROOT->LoadMacro("h2.cpp+"); gROOT->LoadMacro("macro.cpp+"); } then try: [...]$ root trial.cxx and/or: root [0] .x trial.cxx
If you want ROOT to run it automatically on startup, simply rename “trial.cxx” into “rootlogon.C”.

[quote=“Pepe Le Pew”] { gROOT->LoadMacro("h1.cpp+"); gROOT->LoadMacro("h2.cpp+"); gROOT->LoadMacro("macro.cpp+"); } then try: [...]$ root trial.cxx and/or: root [0] .x trial.cxx

Does this mean I don’t have to compile those headers each time I enter the ROOt shell ? I just do [...]$ root trial.cxx and/or: root [0] .x trial.cxx and that will be all ?


I recommend before continuing this thread to read the entire ROOT primer and in particular … oot-macros .


$> root myMacro.C

is equivalent to

root [0] .x myMacro.C

and has the same result of

root [0] .L myMacro.C
root [1] myMacro()


Finally solved it, the last reply to the question: Is there any tutorial on running root entirely with g++? is related to this question and solves the problem. Thanks everyone.