Trying to compile C++ and link to ROOT libraries?

I would like to use some root libraries on my machine, but I seem to run into errors. I have set my path, ld_library_path, and cpath where the appropriate files are.

Here is my command:

When I do this, I get errors such as:

/tmp/ccwRK5kc.o: In function `TApplicationImp::IsA() const': SVD.cc:(.text._ZNK15TApplicationImp3IsAEv[_ZNK15TApplicationImp3IsAEv]+0xb): undefined reference to `TApplicationImp::Class()'

I am not sure what this means. MY CPATH points to the location of TApplication.h. Is there something wrong with my compile command?

Try: mpic++ `root-config --cflags` -O3 -pg -o SVD SVD.cc `root-config --glibs` -lMathMore -lMinuit -lMinuit2

Wow, that seems to have removed all of the ROOT errors! Can you give me a little insight as to why the flags have to be moved around like that?

I still have to figure out why my GSL won’t link. I put the directory /usr/local/include/gsl into my CPATH, but I still get

SVD.cc:(.text.startup+0x3c5): undefined reference to `gsl_matrix_calloc'

My gsl headers are all located in /usr/local/include/gsl. If I only keep the includes and remove all code references, there are no errors, so it appears to be finding them OK. But I cannot call GSL functions such as

  gsl_matrix* Uw = gsl_matrix_calloc(17,17);

In the end of this line try to add something like:
-L/usr/local/lib -lgsl -lgslcblas -lm

What concerns the order of flags, search the linker’s manual for “–as-needed” and “–no-as-needed”.

[quote=“Wile E. Coyote”]In the end of this line try to add something like:
-L/usr/local/lib -lgsl -lgslcblas -lm

What concerns the order of flags, search the linker’s manual for “–as-needed” and “–no-as-needed”.[/quote]

Ok, I will look this up!

The above flags have removed some errors, and added new ones:

mpic++ `root-config --cflags` -O3 -pg -o SVD SVD.cc `root-config --glibs` -lMathMore -lMinuit -lMinuit2 -L/usr/local/lib -lgsl -lgslcblas -lm /usr/local/lib/libgsl.so: undefined reference to `cblas_ztrsv' /usr/local/lib/libgsl.so: undefined reference to `cblas_scasum' /usr/local/lib/libgsl.so: undefined reference to `cblas_izamax' ...and many more...

So do I need to rebuild my GSL? I just built it, but the libgsl.so doesn’t seem to be compiled properly.

Well, maybe you need to add “-lcblas” before “-lm”.

Actually a better idea would be to add (this should automatically list all dependent libraries):
gsl-config --libs
instead of manually adding “-L/usr/… -lm”.

[quote=“Wile E. Coyote”]Actually a better idea would be to add (this should automatically list all dependent libraries):
gsl-config --libs
instead of manually adding “-L/usr/… -lm”.[/quote]

Hmm, this did not change anything.

[code]gsl-config --libs
-L/usr/local/lib -lgsl -lgslcblas -lm

mpic++ root-config --cflags -O3 -pg -o SVD SVD.cc root-config --glibs -lMathMore -lMinuit -lMinuit2 gsl-config --libs
/usr/local/lib/libgsl.so: undefined reference to cblas_ztrsv' /usr/local/lib/libgsl.so: undefined reference tocblas_scasum’
/usr/local/lib/libgsl.so: undefined reference to `cblas_izamax’[/code]

Then try to add “-lblas” or “-llapack -lblas” in the end.

Hmm, this also did not change anything. Is it possible it is not a flag problem?

mpic++ `root-config --cflags` -O3 -pg -o SVD SVD.cc `root-config --glibs` -lMathMore -lMinuit -lMinuit2 `gsl-config --libs` -llapack -lblas /usr/local/lib/libgsl.so: undefined reference to `cblas_ztrsv' /usr/local/lib/libgsl.so: undefined reference to `cblas_scasum' /usr/local/lib/libgsl.so: undefined reference to `cblas_izamax'

And what happens if you add “-lcblas” (instead of “-llapack -lblas”)?

Here is what I get:

mpic++ `root-config --cflags` -O3 -pg -o SVD SVD.cc `root-config --glibs` -lMathMore -lMinuit -lMinuit2 `gsl-config --libs` -lcblas /usr/bin/ld: cannot find -lcblas collect2: error: ld returned 1 exit status make: *** [SVD] Error 1

Ok, I installed libatlas-base-dev and that last option now works. I will check the other flag options again now.

It appears that -lcblas is necessary for this to compile properly.

Many thanks to Wile E. Coyote.

Great.
I think you should only need (i.e. no “-lblas” nor “-lcblas”):
gsl-config --libs
which should generate something like: “-L/usr/local/lib -lgsl -lgslcblas -lm”.
Maybe if you built your GSL in “/usr/local” without “libatlas-base-dev”, then you need “-lcblas”.

[quote=“Wile E. Coyote”]Great.
I think you should only need (i.e. no “-lblas” nor “-lcblas”):
gsl-config --libs
which should generate something like: “-L/usr/local/lib -lgsl -lgslcblas -lm”.
Maybe if you built your GSL in “/usr/local” without “libatlas-base-dev”, then you need “-lcblas”.[/quote]

Without the final -lcblas, I get the most recent errors listed. So perhaps I have not build my GSL correctly but the -lcblas does make it compile.