My program is too slow in interpreter so I want to create an executable file. As an novice of root, I don’t know how I can do it. I read the part of ACLiC but I got confused. In my files, no class is defined. Should I make some declaration before I use “.L try.cpp+” to compile it, if my file name is try.cpp? And how can I use cc or gcc to make an executable file?
Hi,
let’s assume your macro is called “myTry.C”. try.cpp is really a bad name - try is a C++ keyword, and that will bite you later, see below. Also, it’s common to call macros something.C, but the source files which have a corresponding header something.cxx / .cpp.
Let’s start with a simple case: your macro myTry.C looks like this:{
std::cout << "This is " << gROOT->GetName() << std::endl;
}This is called an unnamed macro (because the outer {…} block doesn’t have a name). You can run it with “.x myTry.C”.
If you want to compile a macro, that macro needs to be valid C++. For that, the outer {…} block needs to be a named function, and the #includes need to be added:
#include <iostream>
#include "TROOT.h"
void myTry() {
std::cout << "This is " << gROOT->GetName() << std::endl;
}You can call this macro using the following syntaxes:
Interpret, explicit function call:.L myTry.C
myTry()
Interpret, implicit function call; loads the macro, calls the function with the same name as the macro - this is why a macro try.C is not a good idea (you can’t define a function named “try”):.x myTry.C
Compiled, explicit call of a compiled function:.L myTry.C+
myTry()
Compiled, implicit function call:.x myTry.C+
This also means you can have several methods in one macro, which you can call using
.L myTry.C+
myOtherMethodInMyTry()
All this is also nicely explained in the users guide. This is the recommended way of compiling your code. If you really need to build your own binary, have a look at the Makefile in $ROOTSYS/test.