Compiling a C++ macro using root classes with g++

Hi,
I am trying to compile a simplified C++ macro (test2.C see below please)
in my shell using g++ while this macro uses a TH1D object.
I know how to do this using root by doing
.L test2.C++
and then out of root I just convert the .so file to an .exe file :
g++ test2_c.so -o test2.exe
However, I would like this compilation not to need to launch root.
Is there any way to just compile the code directly in a shell and run the executable?
What should I include?
I tried this by including lib and include directories but it complains with the following output :
[elpc14] /data/Analysis > g++ test2.C -o b.exe
/tmp/ccVWXHqO.o(.text+0x15d): In function main': : undefined reference toTH1D::TH1D(char const*, char const*, int, double, double)’
/tmp/ccVWXHqO.o(.text+0x17c): In function main': : undefined reference toTObject::operator delete(void*)’
/tmp/ccVWXHqO.o(.text+0x1e0): In function main': : undefined reference toTFile::TFile(char const*, char const*, char const*, int)’
/tmp/ccVWXHqO.o(.text+0x211): In function main': : undefined reference toTFile::Close(char const*)’
/tmp/ccVWXHqO.o(.text+0x223): In function main': : undefined reference toTFile::~TFile()’
/tmp/ccVWXHqO.o(.text+0x24d): In function main': : undefined reference toTFile::~TFile()’
/tmp/ccVWXHqO.o(.gnu.linkonce.t._ZN7TObjectnwEj+0xd): In function TObject::operator new(unsigned int)': : undefined reference toTStorage::ObjectAlloc(unsigned int)'
collect2: ld returned 1 exit status
[elpc14] /data/Analysis >

Thanks for any help!

My code is here :

//test2.C
#include <iostream>
#include "/opt/5.14.00f-CMS3q/include/TH1.h"
#include "/opt/5.14.00f-CMS3q/include/TH1D.h"
#include "/opt/5.14.00f-CMS3q/include/TFile.h"
int main(){
  TH1D *hRawMet=new TH1D("Met","Met",50,0.,200.);
  hRawMet->Fill(50.);
  TFile f("output.root","recreate");
  hRawMet->Write();
  f.Close();
  return 0;
}
1 Like

do:

g++ test2.C -o b.exe `root-config --cflags --glibs`
root-config is a small shell script in $ROOTSYS/bin that knows about the necessary compilation flags and libraries. To see what this script is doing type

root-config --cflags --glibs
Rene

2 Likes

Rene,

Is there any way to make the compiled program (b.exe) show graphics?

Using your method I get a shell output instead of getting canvases drawn (almost as if I passed the -b option to root).

You must include a TApplication object , or a TRint (that derives from TApplication) in your main program.
One more reason to NOT WRITE YOUR OWN MAIN PROGRAM.
Use root.exe and call your compiled code from the ROOT prompt.
See the first pages of the Users Guide.

Rene

Hi,

You will need to create a TApplication object (or TRint) object in your main; you may also want to have an event loop so that you can interact with the canvases … (see ROOT’s main.cxx) … You might be better off creating a shared library and loading in the regular root executable.

Cheers,
Philippe.

Hi everybody.

Is there something like root-config for AliRoot?
I’m trying to compile the code bellow, just to get the try…catch block work well (It doesn’t in interpreted code :frowning: )

#include <cstdlib>
#include <exception>
#include <iostream>
#include <Rtypes.h>
#include <AliSimulation.h>
//...a lot more of includes
using namespace std;
int sim(Int_t nev=100) {
   int r=0;
   try {
      //cout << "throwing exception..." << endl;
      //throw exception();
      cout << "Sim.C: Runing simulation with " << nev << " events..." << endl;
      AliSimulation simulator;
      simulator.RunSimulation(nev);
   }
   catch (exception& e){
      cout << "Sim.C: EXCEPTION: e.what()=\"" << e.what() << "\"" << endl;
      r = 1;
   }
   if (r!=0) exit (r);
}

but get erros like:

/home/user/cern/alice/AliRoot/lib/tgt_linuxx8664gcc/libEMCALbase.so: undefined reference to `AliEMCALGeoUtils::ShowMembers(TMemberInspector&, char*)'
/home/user/cern/alice/AliRoot/lib/tgt_linuxx8664gcc/libEMCALsim.so: undefined reference to `AliEMCALShishKebabTrd1Module::fgr'
/home/user/cern/alice/AliRoot/lib/tgt_linuxx8664gcc/libSTEER.so: undefined reference to `TProofOutputFile::OpenFile(char const*)'
/home/user/cern/alice/AliRoot/lib/tgt_linuxx8664gcc/libEMCALbase.so: undefined reference to `AliEMCALGeoUtils::CheckAbsCellId(int) const'
/home/user/cern/alice/AliRoot/lib/tgt_linuxx8664gcc/libEMCALbase.so: undefined reference to `AliEMCALGeoUtils::GetSuperModuleNumber(int) const'

How can I do for get this simulation code compiled?
thanks!

please submit Alice specific questions to the Alice mailing lists.

Rene