# Making shared library for IO

**URL:** <https://root-forum.cern.ch/t/making-shared-library-for-io/52148>\
**Category:** ROOT\
**Created:** [October 29, 2022, 2:28am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148 "2022-10-29T02:28:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxwellcui](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxwellcui/32/31321_2.png) [@maxwellcui](https://root-forum.cern.ch/u/maxwellcui)\
**Post date:** [October 29, 2022, 2:28am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148/1 "2022-10-29T02:28:22Z")

</div>

Hi experts and ROOT friends,

ROOT asked me to provide a dictionary for the vector of vector when I tried to compile the following file `util.cc` which is used in the main function.

I followed the instructions on the official tutorial, generated the `LinkDef.h`, and compiled it using rootcling shown in the Makefile below, and it seems to link successfully to the executable.

However, it failed at run time and I have no idea how to debug from here. The error message is attached in the `log.txt` file. A testing main function is also attached.  
[log.txt](https://root-forum.cern.ch/uploads/short-url/hhOG11DdJwThwnqIQznLgQq4D1A.txt) (5.1 KB)  
[test.cc](https://root-forum.cern.ch/uploads/short-url/wj1elseFeyEmgF2DTHqMTSJDUdk.cc) (242 Bytes)

Please let me know if you could provide any hints or directions.  
Thank you very much!  
Maxwell

_ROOT Version:_ 6.26/04  
_Platform:_ Ubuntu 18.04.1 LTS  
_Compiler:_ g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

* * *

```auto
// util.cc
void prepare(TFile *inputFile)
{
    
    std::vector<std::vector<float>> *GlobalPosition;

    TTree *tree;
    tree = static_cast<TTree *>(inputFile->Get("tree"));

    TBranch *b_GlobalPosition;
    tree->SetBranchAddress("GlobalPosition", &GlobalPosition, &b_GlobalPosition);

    b_GlobalPosition->GetEntry(0);
    std::cout<<"Event size is "<<b_GlobalPosition->GetEntries();

}

```

```auto
//LinkDef.h
#ifdef __CLING__

#pragma link C++ class std::vector < std::vector < float>> + ;

#endif

```

```makefile
#makefile
LIB := $(shell root-config --libs)
CFLAGS := $(shell root-config --cflags)
INC := $(shell root-config --incdir)

run : build
	./test

build: makeLib test

test: src/test.cc libUtilLib.so
	g++ src/test.cc -o $@ -I$(INC) $(LIB) $(CFLAGS) -L$(PWD) -lUtilLib -Wl,-rpath,$(PWD)

makeLib: libUtilLib.so

libUtilLib.so: UtilDict.cxx src/util.cc
	g++ -shared -o $@ -I$(INC) -fPIC $^ 

UtilDict.cxx: ./include/LinkDef.h 	
	rootcling -f $@ $^

clean:
	rm *.so test *Dict*

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 31, 2022, 11:23am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148/2 "2022-10-31T11:23:39Z")

</div>

At least: `static std::vector<std::vector<float>> *GlobalPosition = 0;`

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [October 31, 2022, 11:28am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148/3 "2022-10-31T11:28:47Z")

</div>

In case it helps: [GitHub - eguiraud/root\_dictionaries\_tutorial: A tutorial on creating ROOT dictionaries to perform I/O of custom C++ classes](https://github.com/eguiraud/root_dictionaries_tutorial)

---

<div class="post-metadata">

**Author:** ![maxwellcui](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxwellcui/32/31321_2.png) [@maxwellcui](https://root-forum.cern.ch/u/maxwellcui)\
**Post date:** [November 1, 2022, 3:02am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148/4 "2022-11-01T03:02:02Z")

</div>

Thank you! This indeed solves the problem. Could you explain a bit more why does it need to be a static type? It’s not clear to me.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 1, 2022, 7:30am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148/5 "2022-11-01T07:30:09Z")

</div>

This variable must live as long as the “`tree`”. I don’t see you delete the “`tree`” so the simplest fix was to use “`static`”.  
BTW. The crucial fix was adding “`= 0`”

---

<div class="post-metadata">

**Author:** ![maxwellcui](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxwellcui/32/31321_2.png) [@maxwellcui](https://root-forum.cern.ch/u/maxwellcui)\
**Post date:** [November 1, 2022, 8:01am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148/6 "2022-11-01T08:01:39Z")

</div>

Thank you again for the explanation.  
In this case, why do we have to initialize the pointer to null (besides being a good habit)? Naively, I think tree-\>SetBranchAddress() would take care of it.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 1, 2022, 8:19am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148/7 "2022-11-01T08:19:06Z")

</div>

That pointer must be “initialized” as you could also have “`GlobalPosition = new vector<...>;`” and then ROOT would use the already created object.  
BTW. You should “`delete GlobalPosition;`” once it is no longer needed (regardless if you created it or ROOT did). At the end of your routine, you could either “`delete tree;`” or “`tree->ResetBranchAdresses();`” and then “`delete GlobalPosition;`”. In your simple code, you can skip it as it will not create any big memory leaks, of course.

---

<div class="post-metadata">

**Author:** ![maxwellcui](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxwellcui/32/31321_2.png) [@maxwellcui](https://root-forum.cern.ch/u/maxwellcui)\
**Post date:** [November 1, 2022, 8:47am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148/8 "2022-11-01T08:47:57Z")

</div>

Very reasonable! Thank you very much!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [November 15, 2022, 8:48am UTC](https://root-forum.cern.ch/t/making-shared-library-for-io/52148/9 "2022-11-15T08:48:34Z")

</div>

Closed on November 15, 2022, 8:48am UTC

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