Linking custom classes and namespaces in root macro

ROOT Version: 6.14
Platform: MacOSX
Compiler: Not Provided

I am trying to write a root macro which uses member functions from custom classes and namespaces, All these classes and namespaces are declared in different header files and their definitions are provided in corresponding .cpp files. The reason for doing this is to avoid writing all those classes, functions ad namespaces from scratch in every root macro which needs those functions (there will be more than 1 root file which will be using them).
Now when I try to run the root macro from root interpreter, those functions are not outputting the correct values. My guess is that they are not linked in the root macro.

I have tried creating a shared library of all those files and LinkDef.h file to create a dictionary but it is not working.

My question is how to link those files containing classes and namespaces in the root macro?


how did you create the dictionary, e.g. with what command? Did you for example create rootmap files (rootcling -h or genreflex -h will clarify further)?


I apologize for the late reply. I just created it by running

.x file_name.C++

in the root interpreter. Is this the correct way of doing it?

It created


files though. I don’t know if it is correct and how to proceed from here.


Before loading the macro that uses the custom classes, do

.L <path to>

Which should load the definitions, I think :slight_smile:

Hi Lakshya,

compiling your macro with Aclic is not sufficient.
You need to:

  1. Build a dictionary and a rootmap file for your classes (see rootcling --help)
  2. Build a shared library with the dictionary generated by 1. and the cpp files where the implementations of your classes are.
  3. Update the LD_LIBRARY_PATH env variable to include the directory where the .so, .pcm and .rootmap files are

Let us know how it goes!

Thanks @dpiparo for your reply.
Like you suggested, this is what I did:
(pars.h is the class declaration file which has all the input parameters, decay_dist.h file is the declaration file of my namespace which has all the functions that I need, my analysis.C file uses both these files as well as their implementation *.cpp files. decay_dist.h is also dependent on pars.h.)

genreflex pars.h decay_dist.h --rootmap decay.rootmap --rootmap-lib
g++ -o decay_dist_rflx.cpp pars.cpp decay_dist.cpp -std=c++17 -shared -fPIC `root-config --cflags --libs`

it gave me a warning that

Warning: Unused class rule: pars

My shared library is not getting created which is why I cannot compile and link it.
What am I doing wrong?

Hi Lakshya,

thanks for trying out.
You are missing the selection xml file in the invocation of genreflex. You can add it with the option -s myselection.xml. For the syntax of such file, you can see an example with genreflex --help.


Just to make sure that I understood it. You are saying I need to include -s myselection.xml file but in the example with genreflex --help, it is saying the usage is -s filename. So should I use my class declaration file or linkdef.h? Also, what type of file is myselection.xml?


as you say, the syntax is -s filename where filename is whatever name, for example myselection.xml. For the explanaitions about selection xml files, you can have a look to genreflex --help.


I apologize for asking it again but when I used something like this

genreflex pars.h decay_dist.h -s pars.xml --rootmap decay.rootmap --rootmap-lib

I got an error

Error: rootcling: cannot open linkdef file pars.xml

I copied the template of the XML file from the genreflex --help and put it in a file pars.xml
and ran the above command again. I got this

Error: Malformed tag [<selection> (tag doesn't begin with <)!
Error: At line 2. Bad tag: [<selection>
Error: Parsing XML file pars.xml

I don’t know what’s going on and having a feeling that using shared libraries across the macros won’t work. I’ll just go back to defining classes and namespaces in each of my macros separately.


the error is telling you that the syntax your selection xml has a syntax error. Once that is fixed, it will work.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.