Problems Building a class into a shared library

Hi,
so I need to create a class that I need as a dictionary (to load up in root) and also referenced by a standalone executable that generates a root file, so would need to create this class into an *.so

So I have a simple makefile like this:

[]$ make
g++ -g -c -I/usr/include/root -fpic FillEvents.cpp
g++ -g -c -I/usr/include/root -fPIC Event.cpp
ld -G Event.o -o libEvent.so
g++ -L/usr/lib64/root -L./ -fPIC FillEvents.o -lEvent -lHist -lRIO -lTree -o FillEvents.exe
FillEvents.o: In function main': /home/jade/insight/rootSamples/FillEvents.cpp:72: undefined reference toRTrk::RTrk(unsigned int const&, double const&, double const&, double const&, double const&)‘
FillEvents.o: In function Event': /home/jade/insight/rootSamples/Event.h:18: undefined reference tovtable for Event’
FillEvents.o: In function TBranch* TTree::Branch<Event>(char const*, Event**, int, int)': /usr/include/root/TTree.h:319: undefined reference totypeinfo for Event’
.//libEvent.so: undefined reference to ROOT::GenerateInitInstance(Event const*)' .//libEvent.so: undefined reference toROOT::GenerateInitInstance(RArc const*)’
.//libEvent.so: undefined reference to ROOT::GenerateInitInstance(RTrk const*)' .//libEvent.so: undefined reference tovtable for RNode’
.//libEvent.so: undefined reference to vtable for RArc' .//libEvent.so: undefined reference toROOT::GenerateInitInstance(RNode const*)'
collect2: ld returned 1 exit status

As noted in the users guide concerning “Adding a Class with a Shared Library” section, I have these macros ClassDef and ClassImp in the source code of the class.

Am I just missing some library in the linking of this executable? If so, which library do I need??

Event tired replacing the linking of the executable to (removing the dependency on the shared object and forcing a static link to the Event.o):

FillEvents.exe: FillEvents.o Event.o
g++ -o FillEvents.exe FillEvents.o Event.o root-config --cflags --libs

And I still get the same errors:
[]$ make
g++ -g -c -I/usr/include/root -fpic FillEvents.cpp
g++ -g -c -I/usr/include/root -fPIC Event.cpp
g++ -o FillEvents.exe FillEvents.o Event.o root-config --cflags --libs
FillEvents.o: In function main': /home/jade/rootSamples/FillEvents.cpp:72: undefined reference toRTrk::RTrk(unsigned int const&, double const&, double const&, double const&, double const&)‘
FillEvents.o: In function Event': /home/jade/rootSamples/Event.h:19: undefined reference tovtable for Event’
FillEvents.o: In function TBranch* TTree::Branch<Event>(char const*, Event**, int, int)': /usr/include/root/TTree.h:319: undefined reference totypeinfo for Event’
Event.o: In function RNode': /home/jade/rootSamples/Event.cpp:27: undefined reference tovtable for RNode’
/home/jade/rootSamples/Event.cpp:33: undefined reference to vtable for RNode' /home/jade/rootSamples/Event.cpp:40: undefined reference tovtable for RNode’
Event.o: In function RArc': /home/jade/rootSamples/Event.cpp:105: undefined reference tovtable for RArc’
/home/jade/rootSamples/Event.cpp:114: undefined reference to vtable for RArc' Event.o: In function__static_initialization_and_destruction_0’:
/home/jade/rootSamples/Event.cpp:8: undefined reference to ROOT::GenerateInitInstance(Event const*)' /home/jade/rootSamples/Event.cpp:9: undefined reference toROOT::GenerateInitInstance(RNode const*)’
/home/jade/rootSamples/Event.cpp:10: undefined reference to ROOT::GenerateInitInstance(RArc const*)' /home/jade/rootSamples/Event.cpp:11: undefined reference toROOT::GenerateInitInstance(RTrk const*)'
collect2: ld returned 1 exit status
make: *** [FillEvents.exe] Error 1

So it looks like this is an implementation problem though it results in a linking issue(?). Aside from adding the ClassDef and ClassImp inside my Event.cpp/Event.h (not to be mistaken to the source code examples given in the document, though I followed the examples there), what else am I missing to get a class that is used to write events to a root file and used also as a dictionary???

See:
http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch20s02.html
ls ${ROOTSYS}/test
ls ${ROOTSYS}/share/doc/root/test

Hi Pepe,
I did look at all those examples (e.g. the Event.cxx/Event.h and the documents discussing that), but I’m still at a loss as to what’s wrong. The classes I created is very close to the Event.cxx/Event.h case in the document… but I’m still get these errors and I’m not getting any clues from the documents and examples in those test directories you referred to as to what I’m doing wrong.

Run “make” in the “test” subdirectory and see, for example, how the “Even(MT)” is being built.

Hi Pepe,
OK, I figured it out. Thanks!

Jade

Hello,

I encountered the same problem, unfortunately I havent been able to solve it yet. Can you guys give me a hand please? I am not that experienced thus sorry if this is trivial.

I have a simple class rawEvent in .h and .cxx files (ClassDef, ClassImp in them). I looked into the root/test library on the EventMT building and did this.

  1. g++ -O2 -Wall -fPIC -pthread -std=c++11 -Wno-deprecated-declarations -m64 -I/home/matous/work/root6/include -c rawEvent.cxx

  2. Then I created EventLinkDef.h with:

#ifdef MAKECINT

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;

#pragma link C++ class rawEvent+;

#endif

and on cmd typed
rootcling -f rawEventDict.cxx -c rawEvent.h EventLinkDef.h

  1. g++ -O2 -Wall -fPIC -pthread -std=c++11 -Wno-deprecated-declarations -m64 -I/home/matous/work/root6/include -c rawEventDict.cxx

  2. g++ -shared -O2 -m64 rawEvent.o rawEventDict.o -o librawEvent.so

However when I try to compile my file with the main function
g++ -g -o SciFi_ana SciFi_ana.cxx root-config --cflags root-config --glibs
I got the same error
/home/matous/work/na61/analysis/rawEvent.h:13: undefined reference to `vtable for Event’

.so was not done well because when I tried to open the .so in root I got the following error:

cling::DynamicLibraryManager::loadLibrary(): /home/matous/work/na61/analysis/librawEvent.so: undefined symbol: _ZN4ROOT20GenerateInitInstanceEPK5Event

I am not sure what I am doing wrong. Can somebody help? Thanks in advance.

Mat

Hi,

You probably need to link you binary against librawEvent.so

Cheers, Axel.