# Simple way to create and merge shared libraries?

**URL:** <https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652>\
**Category:** ROOT\
**Created:** [January 9, 2011, 1:23am UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652 "2011-01-09T01:23:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rene](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/7cd45c/32.png) [@rene](https://root-forum.cern.ch/u/rene)\
**Post date:** [January 9, 2011, 1:23am UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/1 "2011-01-09T01:23:58Z")

</div>

Hello,  
I have n TObject-derived classes (First.C, First.h and Second.C, Second.h etc.) and I would like to create a shared library containing all of them. How can I do this in a simple way? Since I use a Makefile for the rest of my analysis code, I thought a manual g++ing could be the solution, but I have no clue on how to achieve this, and perhaps there is some trick as simple as “.L First.C++”…

Thank you in advance!

---

<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:** [January 9, 2011, 2:21pm UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/2 "2011-01-09T14:21:32Z")

</div>

## VIVE L’AMOUR! how about you create a file “MyCode.cxx” which contains:

#include “First.C”  
#ifdef **MAKECINT**  
#pragma link C++ defined\_in “First.h”;  
#pragma link C++ defined\_in “First.C”;  
#endif

#include “Second.C”  
#ifdef **MAKECINT**  
#pragma link C++ defined\_in “Second.h”;  
#pragma link C++ defined\_in “Second.C”;  
#endif

#include “Third.C”  
#ifdef **MAKECINT**  
#pragma link C++ defined\_in “Third.h”;  
#pragma link C++ defined\_in “Third.C”;  
#endif

## void MyCode(void) {} // “dummy” startup routine

Then you simply do “.L MyCode.cxx++”, or “.x MyCode.cxx++” at the ROOT prompt.  
This will create “MyCode\_cxx.so”, but you can simply rename it into “MyCode.so” or “libMyCode.so” and use it as any other shared library afterwards. ![:-({|=](https://root-forum.cern.ch/uploads/default/original/2X/e/e74005e7ece145fd25c262fc4b22b15cec9c8abb.gif "Boo hoo!")  
One can easily write a simple shell script which will automatically create “MyCode.cxx” from a given set of “filenames” (given as parameters to this shell script, e.g. “MyCode.sh”, something like “sh MyCode.sh First Second Third” ), or one could even write a simple ROOT macro which would do the same thing (including creation of the shared library itself). \<img src="/uploads/default/original/2X/8/84c2fe9464a4066c00e1bd5978e913e7869cbb07.gif" width=“22” height=“16” alt=":-"" title=“Whistle”/\>

I am stupid. No?  
Pepe Le Pew.

---

<div class="post-metadata">

**Author:** ![rene](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/7cd45c/32.png) [@rene](https://root-forum.cern.ch/u/rene)\
**Post date:** [January 11, 2011, 9:25pm UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/3 "2011-01-11T21:25:39Z")

</div>

This does not work, since my classes inherit from TObject, and I’d have to remove ClassImp from all but one of the sources…

---

<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:** [January 11, 2011, 9:52pm UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/4 "2011-01-11T21:52:08Z")

</div>

VIVE L’AMOUR!  
as far as I remember, you need “ClassDef” / “ClassImp” macros for a class only if you want to use ROOT I/O on its objects. So, if you do NOT need it, you can remove them (even if the class inherits from TObject).  
If you need ROOT I/O on its objects, then I have no simple solution. In this case I would create a hand-written “linkdef” file (containing “#pragma” lines for all classes, etc.), parse it with rootcint, then compile every source code file separately and finally create the shared library.  
A pitiful case, am I not?  
Pepe Le Pew.

---

<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:** [January 11, 2011, 10:09pm UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/5 "2011-01-11T22:09:58Z")

</div>

Maybe you can also try this simple trick … execute the following shell commands …

cat First.h Second.h Third.h \> MyCode.hxx  
echo ‘#ifndef **MAKECINT** ’ \>\> MyCode.hxx  
echo '#include “First.C” ’ \>\> MyCode.hxx  
echo '#include “Second.C” ’ \>\> MyCode.hxx  
echo '#include “Third.C” ’ \>\> MyCode.hxx  
echo ‘#endif’ \>\> MyCode.hxx

Then try:  
[…] \> root  
root [0] .L MyCode.hxx++

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [January 20, 2011, 3:05am UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/6 "2011-01-20T03:05:39Z")

</div>

[quote]as far as I remember, you need “ClassDef” / “ClassImp” macros for a class only if you want to use ROOT I/O on its objects. So, if you do NOT need it, you can remove them (even if the class inherits from TObject).[/quote]Please note that **neither** ClassDef nor ClassImp is needed to to ROOT I/O. All you need for ROOT I/O is to generate the dictionary for the class.

Note that ClassDef provides an optimizations, object with a ClassDef will be smaller on file and will be read slightly faster. Also customizing schema evolution would be easier (since the ‘old version’ can easily be refered to with the ClassDef number they had).

Note that ClassImp is need only if you want to be able to produce the documentation for your class using THtml.

Note that inheriting from TObject (which also requires the use of ClassDef) allows your object to be stored in the ROOT collection, in particular in a TClonesArray; However with or without the inheritance from TObject you can store the object in a TTree (i.e. anything with a dictionary can go in a TTree).

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [January 20, 2011, 3:06am UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/7 "2011-01-20T03:06:52Z")

</div>

Hi,

[quote]This does not work, since my classes inherit from TObject, and I’d have to remove ClassImp from all but one of the sources…[/quote]Why? How does it fails? Did you retry with v5.28?

Philippe.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [January 20, 2011, 3:09am UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/8 "2011-01-20T03:09:13Z")

</div>

[quote]Since I use a Makefile for the rest of my analysis code, I thought a manual g++ing could be the solution[/quote]You need to generate, compile and link a dictionary. Use a linkdef.h file:`#ifdef __MAKECINT__
#pragma link C++ class First+;
#pragma link C++ class Second+;
#pragma link C++ class Third+;
#endif`and `rootcint -f dict.cxx -c First.h Second.h Third.h linkdef.h`  
and finally compile and link the resulting dict.cxx

Cheers,  
Philippe.

---

<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:** [January 20, 2011, 7:20am UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/9 "2011-01-20T07:20:01Z")

</div>

VIVE L’AMOUR!  
well, it seems I misinterpreted some of the statement that I found in the documentation a long time ago (or maybe things changed significantly with time).

Would it be possible that you add your explicit notes about the usage of “TObject”, “ClassDef” and “ClassImp” to the “Motivation” section in the “Adding a Class” chapter of the “User’s Guide”?

I am stupid. No?  
Pepe Le Pew.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [January 20, 2011, 4:08pm UTC](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/10 "2011-01-20T16:08:09Z")

</div>

Will do.

Philippe.
