# Generating dictionaries

## Generating dictionaries

A dictionary (“reflection database”) contains information about the types and functions that are available in a library.

With a dictionary you can call functions inside libraries. Dictionaries are also needed to write a class into a ROOT file (→ see ROOT files).

A dictionary consists of a source file, which contains the type information needed by Cling and ROOT’s I/O subsystem. This source file needs to be generated from the library’s headers and then compiled, linked and loaded. Only then does Cling and ROOT know what is inside a library.

There are two ways to generate a dictionary:

• using ACLiC

• using rootcling

### Using ACLiC to generate dictionaries

With a given header file MyHeader.h, ACliC automatically generates a dictionary:

      root[] .L MyHeader.h+


### Using rootcling to generate dictionaries manually

You can manually create a dictionary by using rootcling:

• DictOutput.cxx Specifies the output file that will contain the dictionary. It will be accompanied by a header file DictOutput.h.

• OPTIONS are:

• Isomething: Adding an include path, so that rootcling can find the files included in Header1.h, Header2.h, etc.

• DSOMETHING: Define a preprocessor macro, which is sometimes needed to parse the header files.

• Header1.h Header2.h...: The headers files.

• Linkdef.h: Tells rootcling, which classes should be added to the dictionary, → see Selecting dictionary entries: Linkdef.h.

Note

Dictionaries that are used within the same project must have unique names.

Compiled object files relative to dictionary source files cannot reside in the same library or in two libraries loaded by the same application if the original source files have the same name. Example

In the first step, a TEvent and a TTrack class is defined. Next an event object is created to add tracks to it. The track objects have a pointer to their event. This shows that the I/O system correctly handles circular references.

In the second step, a TEvent and a TTrack call are implemented.
After that you can use rootcling to manually generate a directory. This generates the eventdict.cxx file.

Implementation of TEvent and TTrack class

Using rootcling to generate the dictionary

eventdict.cxx - the generated dictionary

To select which types and functions should go into a dictionary, create a Linkdef.h file that you use when you call rootcint manually. The Linkdef.h file is passed as the last argument to rootcint. It must end on Linkdef.h, LinkDef.h, or linkdef.h. For example, My_Linkdef.h is correct, Linkdef_mine.h is not.

The Linkdef.h file contains directives for rootcint, for what a dictionary should be created: select the types and functions that will be accessible from the prompt (or in general through CINT) and for I/O.

### Preamble: deselection

A Linkdef.h file starts with the following preamble:

#ifdef __CINT__


The first line protects the compiler from seeing the rootcint directives. The rootcint directives are in the form of #pragma statements. A #pragma link of all something says that by default, rootcint should not generate the dictionary for anything it sees.

The nested classes directive tells rootcint not to ignore nestedclasses, this is, classes defined inside classes like here:

class Outer {
public:
class Inner {
public:
// we want a dictionary for this one, too!
...
};
...
};


### Selection

In the next step, tell rootcint for which objects the dictionary should be generated for:

#pragma link C++ class AliEvent+;
#pragma link C++ function operator+(const TString&amp;,const TString&amp;);


Note

**The + after the class name: This enables an essential feature for rootcint. It is not a default setting, so you must add +at the end.

### Selection by file name

Sometimes it is easier to say: Create a dictionary for everything defined in the MyHeader.h file.
Write the following statement into the Linkdef.h file:

#pragma link C++ defined_in "subdir/MyHeader.h";


Make sure that subdir/MyHeader.h corresponds to one of the header files that is passed to rootcint.

### Closing

Add the following line at the end of the Linkdef.h file:

#endif /* __CINT__ */.


### Example of a Linkdef.h file

#ifdef __CINT__
#endif


## Embedding the rootcling call into a GNU Makefile

Use the following statement to compile and run the code.

.L MyCode.C+


If you need to use a Makefile, there is the following rule for generating a dictionary (see code snippet below). It will create a new source file, which you should compile like all the other sources in your library. In addition, you need to add the include path for ROOT, and you might have to link against ROOT’s libraries: libCore.

This rule generates the rootcling dictionary for the headers $(HEADERS) and a library containing the dictionary and the compiled $(SOURCES):

MyDict.cxx: $(HEADERS) Linkdef.h [TAB] rootcling -f$@ -c $(CXXFLAGS) -p$^
libMyLib.so: MyDict.cxx $(SOURCES) [TAB] g++ -shared -o$@ root-config --ldflags $(CXXFLAGS) -I$(ROOTSYS)/include \$^