# Problems Building a class into a shared library

**URL:** <https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758>\
**Category:** Cling\
**Created:** [August 14, 2012, 5:29pm UTC](https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758 "2012-08-14T17:29:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jade2](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jade2](https://root-forum.cern.ch/u/jade2)\
**Post date:** [August 14, 2012, 5:29pm UTC](https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758/1 "2012-08-14T17:29:15Z")

</div>

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](http://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 to`RTrk::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 to`vtable for Event’  
FillEvents.o: In function `TBranch* TTree::Branch<Event>(char const*, Event**, int, int)': /usr/include/root/TTree.h:319: undefined reference to`typeinfo for Event’  
.[//libEvent.so](https://libEvent.so): undefined reference to `ROOT::GenerateInitInstance(Event const*)' .//libEvent.so: undefined reference to`ROOT::GenerateInitInstance(RArc const\*)’  
.[//libEvent.so](https://libEvent.so): undefined reference to `ROOT::GenerateInitInstance(RTrk const*)' .//libEvent.so: undefined reference to`vtable for RNode’  
.[//libEvent.so](https://libEvent.so): undefined reference to `vtable for RArc' .//libEvent.so: undefined reference to`ROOT::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??

---

<div class="post-metadata">

**Author:** ![jade2](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jade2](https://root-forum.cern.ch/u/jade2)\
**Post date:** [August 14, 2012, 6:08pm UTC](https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758/2 "2012-08-14T18:08:00Z")

</div>

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 to`RTrk::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 to`vtable for Event’  
FillEvents.o: In function `TBranch* TTree::Branch<Event>(char const*, Event**, int, int)': /usr/include/root/TTree.h:319: undefined reference to`typeinfo for Event’  
Event.o: In function `RNode': /home/jade/rootSamples/Event.cpp:27: undefined reference to`vtable for RNode’  
/home/jade/rootSamples/Event.cpp:33: undefined reference to `vtable for RNode' /home/jade/rootSamples/Event.cpp:40: undefined reference to`vtable for RNode’  
Event.o: In function `RArc': /home/jade/rootSamples/Event.cpp:105: undefined reference to`vtable 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 to`ROOT::GenerateInitInstance(RNode const\*)’  
/home/jade/rootSamples/Event.cpp:10: undefined reference to `ROOT::GenerateInitInstance(RArc const*)' /home/jade/rootSamples/Event.cpp:11: undefined reference to`ROOT::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???

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [August 14, 2012, 6:09pm UTC](https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758/3 "2012-08-14T18:09:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jade2](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jade2](https://root-forum.cern.ch/u/jade2)\
**Post date:** [August 14, 2012, 6:22pm UTC](https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758/4 "2012-08-14T18:22:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [August 14, 2012, 6:38pm UTC](https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758/5 "2012-08-14T18:38:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jade2](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jade2](https://root-forum.cern.ch/u/jade2)\
**Post date:** [August 14, 2012, 10:07pm UTC](https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758/6 "2012-08-14T22:07:21Z")

</div>

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

Jade

---

<div class="post-metadata">

**Author:** ![Matous](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/aeb1de/32.png) [@Matous](https://root-forum.cern.ch/u/Matous)\
**Post date:** [July 15, 2016, 1:38pm UTC](https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758/7 "2016-07-15T13:38:10Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [July 15, 2016, 1:58pm UTC](https://root-forum.cern.ch/t/problems-building-a-class-into-a-shared-library/14758/8 "2016-07-15T13:58:37Z")

</div>

Hi,

You probably need to link you binary against [librawEvent.so](http://librawEvent.so)

Cheers, Axel.
