Running C++ with ROOT libraries- how to load .so libraries?

Hi,

I’m running a C++ source with ROOT libraries, and I’m trying to load libEvent.so from the root test folder.
Here is my code:

[code]#include <TFile.h>
#include <TTree.h>
#include
#include <TROOT.h>
#include <TF1.h>
#include <TObject.h>
#include <TStorage.h>
#include <TRint.h>
#include <TSystem.h>
#include <TStyle.h>
#include <TRandom.h>
#include <TCanvas.h>
#include <TF1.h>
#include “TClonesArray.h”
#include “/home/toma/root/test/Event.h”

using namespace std;

int main(){

//gROOT->SetBatch(true);

//gSystem->Load(“libEvent”);

Track t;

}
[/code]

Here is also the Makefile for the source I’m trying to compile (Mysource.cpp):

LIBS    =`root-config --libs`
INCS    =`root-config --cflags`
CCOPTS  =-g $(LIBS) $(INCS)
F_FLAGS =-g -fno-globals -fno-automatic -finit-local-zero -fno-second-underscore -fno-backslash -fno-globals -ff90 -fpic
C_FLAGS = -O2 -Wall -fPIC $(INCS)


MySource: MySource.o 
	g++  -L Event MySource.o -o MySource -$(CCOPTS) 

.cpp.o:
	g++  -L Event $(C_FLAGS) -c $< -o $*.o

As you can see, I have tried to work with gROOT and gSystem, but have had no success. I keep getting the following error message:

MySource.o: In function main': MySource.cpp:(.text+0x113): undefined reference tovtable for Track’
MySource.cpp:(.text+0x151): undefined reference to `Track::Clear(char const*)'
collect2: ld returned 1 exit status
make: *** [MySource] Error 1

My purpose here is to build my own classes, and create a TTree with objects of those classes. However, I need ROOT to recognize those classes and I need to somehow do that using g++.
I’m working with root version 5.30
Any help would be very appreciated.

Regards

Hi,
The problem is that g++ -L Event MySource.o -o MySource -$(CCOPTS) was probably meant to be g++ libEvent.so MySource.o -o MySource -$(CCOPTS) or g++ -Lwhere_libEvent.so_is -lEvent MySource.o -o MySource -$(CCOPTS)

Cheers,
Philippe.

Hi, Phillippe,

Thank you for your answer.
I’m sorry, I had forgotten to mention that the folder containing libEvent.so was exported in LD_LIBRARY_PATH.
Anyway, I have also tried using

g++  libEvent.so MySource.o -o MySource -$(CCOPTS) 

and I get the same result: the code compiles, but when i try to run it, I get the vtable errors. Are gSystem and gROOT supposed to work if the code is compiled with g++?

Thanks again,
Catalin

Hi,

The issue seems to be that you have not generated (nor compiled or linked) the dictionary for the Event and Track class. I.e. you need something like:rootcint -f EventDict.cxx -c Event.h EventLinkDef.h

[quote]Are gSystem and gROOT supposed to work if the code is compiled with g++?[/quote]Humm … yes of course. However gSystem->Load can not be (by definition of the language) use to load library for code that is needed explicitly from a compilation unit. For example:int main(){ gSystem->Load("libEvent"); Track t; }can not work because the linker needs the symbols for Track at link time …

Also to get gROOT and gSystem to ‘work’ in your compiled code you need to link against the library that are defined in (i.e. you need to have $(LIBS) on you main link line.

Cheers,
Philippe.

Hello

Once again, thank you Philippe for your response. I’m afraid I still have a few problems.
One would be that I do

rootcint -f EventDict.cxx -c Event.h EventLinkDef.h

when I create the shared object library. Do I need to do that again when i compile my source code? Or is it enough to compile everything using said .so library?

as for making gROOT ‘work’, I have tried the following:

 g++  -L Event $(LIBS) $(C_FLAGS) -c $< -o $*.o

and I still get compiling errors of the type:
(.text+0x1a68): undefined reference to `Evnt::Evnt()'
I think I am ommitting some libraries, I just don’t know which ones.
Once again, any help would be appreciated

Cheers,
Catalin

Hi,

[quote]Do I need to do that again when i compile my source code? Or is it enough to compile everything using said .so library?[/quote]You need to do something equivalent for each class (i.e. each class must have its dictionary (dictionary can be combined though)).

code: undefined reference to `Evnt::Evnt()’[/code]For this one, your might have forgot to implement the default constructor of your class (Evnt).

Cheers,
Philippe.

Hi Philippe,

The classes have a combined dictionary. I have tested the compiled shared library with CINT (i.e. started root,
used .L libEvent.so and worked a bit with some class objects) and everything seemed to work fine on that end.

As for the Evnt class, it has an empty constructor with no arguments. Is that a good default constructor?

Thanks,
Catalin

Hi,

[quote]As for the Evnt class, it has an empty constructor with no arguments. [/quote]The default constructor should still assign good default value to all the data member, in particular pointers must be set (to zero).

The linker claims that no implementation of this default constructor was provided at all.

Philippe.

Hello,

I have set all pointers to 0 in the constructors. I still get the same errors (‘undefinefed reference…’). What I had failed to mention before is that I get those ‘undefined reference’ errors for all the methods of all classes.
This happens even when I include the corresponding header files in the source that I am trying to compile.
Could there be another cause for this?

Thanks,
Catalin

[quote] before is that I get those ‘undefined reference’ errors for all the methods of all classes.[/quote]Then the libraries implementing this function are simply not added to your link line.

Philippe.

Hi

Philippe, you were right, the linking was not done properly. I’ve managed to solve that problem.
Thank you very much for your support.

Catalin