Probems linking ROOT libraries with g++

I’m new to ROOT and I have recently set it up on my own Linux machine (Ubuntu 14.04) because I need ROOT and its libraries to build some programs for data analysis.

Right now I can’t link ROOT libraries to C++ code using g++ compiler, while I’m able to run the same code with CINT and as ROOT macro.
I started following the basic guide:
http://www.hep.shef.ac.uk/teaching/phy6040/ROOT/ROOTseminars/Seminar_4.html,
trying from the very basic example:

#include <iostream>
#include <TRandom1.h>

int main(void) {
  TRandom1* myrand = new TRandom1();
  for(int i=0;i<10;++i) {
    std::cout << myrand->Gaus(5,1) << std::endl;
  }
  return 0;
}

I am able to compile the code with the following:

But when I try to create the executable typing:

I get:

I understand that this means that g++ isn’t able to find ROOT libraries, but I thought that the option root-config --glibs should give to g++ the address to all requred libraries.

I know that my problem is to find the right options to give to g++ to compile, because running the Makefile in $ROOTSYS/test, my computer compiled the test code properly and was able to produce the executable, but looking at the test/Makefile I wasn’t able to understand the procedure.

Thanks in advance, I searched a lot on forums and internet and I wasn’t able to find anything that could help me

MP

Try:
root-config --cxx --cflags -o RootExample RootExample.cpp root-config --glibs

It worked, thank you very much!

But I don’t understand the logic of the command I typed (where do I call the compiler?).
How do I modify it to add other libraries? In my project I will use Opencv and boost libraries too

MP

The order of the libraries and object files in the link statement plays a role.

Thanks again

I solved my problem and just for completeness (in case anybody will ever have my same doubt), I want to point out that typing:

I got the right compilation command for my computer settings.
I just added the other headers and libraries I needed for my project keeping the same order.

[quote=“MirkoPitt”]Thanks again

I solved my problem and just for completeness (in case anybody will ever have my same doubt), I want to point out that typing:

I got the right compilation command for my computer settings.
I just added the other headers and libraries I needed for my project keeping the same order.[/quote]

What if I have three files h1.h, h1.cpp and macro.cpp ? How do I link them ? Suppose I have already compiled and created h1.o and macro.o.

Add the list of libraries after the list of your files, which is generated with root-config --glibs

Actually, it should be … add the list of your own source code files (if any), your own object files (if any) and your own libraries (if any) BEFORE the list which is generated by root-config --glibs.