How to compile my code

Hi,
the great thing about ROOT is that it keeps making you a newbie every few years.
I used to be able have a working macro for ROOT and a compilable version that would have run on its own:

#ifdef __CINT__ 

void wth() { // do it easy

#else
#include "TH1F.h"

  int main () { // do it fast

#endif

  TH1F* aPointer = new TH1F("a","a",10,0.,1.);
  aPointer->Draw();
  return 0;
}

but I have now ROOT 6.14 and it doesn’t work, CINT is undefined:
root [0] .X wth.cc
warning: cannot find function ‘wth()’; falling back to .L

Looking at post, I found one that seemed to suggest to use ROOTCLING instead, but it doesn’t seem to work either.

Oh, and it doesn’t compile too!

 set command = "g++ -g -I$ROOTSYS/include `root-config --libs` $1 -o $program"
echo -n "compiling......."
$command
echo "done!"

compiling......./tmp/cc8kpW7w.o: In function `main':
/home/acal/SHiP_tb2018/wth.cc:12: undefined reference to `TH1F::TH1F(char const*, char const*, int, double, double)'
/home/acal/SHiP_tb2018/wth.cc:12: undefined reference to `TObject::operator delete(void*)'
/tmp/cc8kpW7w.o: In function `__static_initialization_and_destruction_0(int, int)':
/cern/root/include/TVersionCheck.h:42: undefined reference to `TVersionCheck::TVersionCheck(int)'
/tmp/cc8kpW7w.o: In function `TObject::operator new(unsigned long)':
/cern/root/include/TObject.h:152: undefined reference to `TStorage::ObjectAlloc(unsigned long)'
collect2: error: ld returned 1 exit status
done!

any help much appreciated!

Here’s another funny things, the posting process removed the double underscores before and after CINT and ROOTCLING. But they are there, trust me…

#include "TH1.h"

// "ROOT Script" entry point.
void wth() {
  TH1F *aPointer = new TH1F("a", "a", 10, 0., 1.);
  aPointer->Draw();
}

#if !defined(__CINT__) && !defined(__CLING__) && !defined(__ACLIC__)
// "Standalone Application" entry point.
// `root-config --cxx --cflags` -o wth wth.cc `root-config --libs`
int main()
{
  wth();
  return 0;
}
#endif /* !defined(__CINT__) && !defined(__CLING__) && !defined(__ACLIC__) */

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

Thanks! Also for the “root-config -cxx…” recipe. I should have known about that…

Hi,
a bit of context: the big change between ROOT5 and ROOT6 was the switch from a homebrew C++ interpreter (CINT) to a clang-based interpreter (better standard compliance, support for all C++ features and newer C++ standards, etc).

With that switch, __CINT__ was substituted by __CLING__

Cheers,
Enrico

P.S. In ROOT6, I usually write ROOT tutorials just like this (works both as macro and as compiled code):

// wth.cpp

#include "TH1.h"

void wth() {
  TH1F *aPointer = new TH1F("a", "a", 10, 0., 1.);
  aPointer->Draw();
}

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

My macro is supposed to be equally valid for both versions, ROOT 5 and ROOT 6, hence I explicitly use both defines.