# Shared library stopped working when external functions were added

**URL:** <https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838>\
**Category:** ROOT\
**Tags:** python, cling, root\
**Created:** [March 5, 2021, 11:54am UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838 "2021-03-05T11:54:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fsili](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@fsili](https://root-forum.cern.ch/u/fsili)\
**Post date:** [March 5, 2021, 11:54am UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/1 "2021-03-05T11:54:11Z")

</div>

Dear experts,

I am having trouble creating a shared library that I want to use in Python, and this problem appears when I call functions in other .cxx file that don’t belong to a class.  
Since I don’t have permission to share all the code, I’ll just schematize which is the problem.

I have the following package structure:

```auto
PackageName
  |
  - lib/
  - obj/
  - utils
  | |
  | - MyMasterFile.py
  | 
  - Root
  | |
  | - ClassA.cxx
  | - ClassB.cxx
  | - UtilsFunctions.cxx
  | - LinkDef.h
  | 
  - Package
  | |
  | - ClassA.h
  | - ClassB.h
  | - UtilsFunctions.h
  | - ProgressBar.h
  | 
  - Makefile

```

The source file UtilsFunctions contains functions that will be called from ClassB and maybe from other python files (if that’s possible). In addition to that, ClassB will always be called from ClassA. So here’s the calling structure:

```auto
//============================================================
// In Root/ClassA.cxx
#include "ClassA.h"
void ClassA::doSomething()
{
    ClassB foo();
    foo.doSomethingInB();
}
//============================================================
// In Package/ClassA.h
#ifndef CLASSA_H
#define CLASSA_H
#include "Package/ClassB.h"
class ClassA {
public:
   void doSomething();
}
#endif

//============================================================
// In Root/ClassB.cxx
#include "ClassB.h"
void ClassB::doSomethingInB()
{
    float val = Function1();
}
//============================================================
// In Package/ClassB.h

#ifndef CLASSB_H
#define CLASSB_H
#include "Package/UtilsFunctions.h"
class ClassB {
public:
   void doSomethingInB();
}
#endif

//============================================================
// In Root/UtilsFunctions.cxx
void Function1()
{
    # calculate something here
}
//============================================================
// In Package/UtilsFunctions.h
#ifndef UTILSFUNCTIONS_H
#define UTILSFUNCTIONS_H
void Function1()
#endif

```

```auto
# In utils/MyMasterFile.py

import ROOT
ROOT.gSystem.Load("libPackageName.so")

tool = ROOT.ClassA()
tool.doSomething()

```

The `LinkDef.h` file contains the following:

```auto
#ifdef __CINT__
#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ nestedclass;

#pragma link C++ class ClassA+;
// #pragma link C++ class ClassB+;
// #pragma link C++ defined_in "UtilsFunctions.h"; (this I don't know if I need to comment or not)

#endif

```

And finally, in my Makefile I compile all `.cxx` files to object files, then the dictionary is created and it’s all linked. All compilation goes well, but when I run the python file I get a message (pointing to ClassA in the `.py` file) saying that ClassA is not defined:

```auto
cling::DynamicLibraryManager::loadLibrary(): /path/to/tool/PackageName/lib/libPackageName.so: undefined symbol: _ZTIN4ROOT8Internal20TTreeReaderValueBaseE
Traceback (most recent call last):
  File "utils/MyMasterFile.py", line 64, in <module>
    main()
  File "utils/MyMasterFile.py", line 52, in main
    tool = ClassA()
NameError: global name 'ClassA' is not defined

```

I tried before adding UtilsFunctions and everything run fine, so this problem appears when I add these functions. I believe it’s something related with how I generated the dictionary, or compilation, but I can’t decipher it. Also I leave my `Makefile` attached (I added the .txt extension so I can upload it )[Makefile.txt](https://root-forum.cern.ch/uploads/short-url/kXtd4p638j438cUXVyolTL15pTV.txt) (1.8 KB).

So to conclude, the question is: what can I do to get this thing working using UtilsFunctions?, because I know it works when I don’t add those

Thank you very much in advance!  
Cheers!  
Francisco

* * *

_ROOT Version:_ 6.20/06  
_Platform:_ lxplus

* * *

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [March 5, 2021, 10:50pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/2 "2021-03-05T22:50:55Z")

</div>

Hi @fsili,

IMHO, the problem is due to a symbol that cannot be resolved, as seen in this line:

```auto
cling::DynamicLibraryManager::loadLibrary(): /path/to/tool/PackageName/lib/libPackageName.so: undefined symbol: _ZTIN4ROOT8Internal20TTreeReaderValueBaseE

```

The previous symbol sems to be exported by `libTreePlayer.so`. Can you please add `-lTreePlayer` in your Makefile and see if it solves the problem?, i.e.

```auto
LINKLIBSIN = -L$(shell root-config --libdir) -lCore -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lGui -lRooStats -lRooFit -lHistFactory -lXMLParser -pthread -lRooFitCore -lTreePlayer -lm -ldl -lFoam -lMinuit -Llib

```

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![fsili](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@fsili](https://root-forum.cern.ch/u/fsili)\
**Post date:** [March 8, 2021, 2:42pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/3 "2021-03-08T14:42:16Z")

</div>

Hi @jalopezg ,  
Thank you very much for your reply! Indeed that solved that problem, but another one similar appeared. This time it reads:

```auto
python: symbol lookup error: /path/to/tool/PackageName/lib/libPackageName.so: undefined symbol: _ZN12NtupleLooper7m_utilsEv

```

where `NtupleLooper` is my ClassB.

Thank you very much,  
Cheers,  
Francisco

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [March 8, 2021, 2:47pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/4 "2021-03-08T14:47:01Z")

</div>

Are you linking the corresponding object generated for `ClassB`? Maybe you left that commented in the `LinkDef.h` file?

---

<div class="post-metadata">

**Author:** ![fsili](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@fsili](https://root-forum.cern.ch/u/fsili)\
**Post date:** [March 8, 2021, 2:49pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/5 "2021-03-08T14:49:19Z")

</div>

Yes. I tried both with that line commented and not commented 😕  
However, I think that this is strange because the python file never calls ClassB. This class is only called from within ClassA.

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [March 8, 2021, 2:56pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/6 "2021-03-08T14:56:52Z")

</div>

The relevant parts of the `Makefile` also look good to me.

Can you copy here the declaration of `NtupleLooper::m_utils`? Maybe is it a non-instance, i.e. `static`, member that was declared but not defined?  
-just making a guess, but it is hard without the sources-

---

<div class="post-metadata">

**Author:** ![fsili](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@fsili](https://root-forum.cern.ch/u/fsili)\
**Post date:** [March 8, 2021, 4:33pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/7 "2021-03-08T16:33:26Z")

</div>

I’ve just corrected that issue. The problem was that I changed the code after posting it here. I put the functions inside UtilsFunctions into a class. Now that I changed it back to plain functions it’s working smoothly.

However, when changing everything back to functions, I’m not able to call them (functions from UtilsFunctions) from the master python script. For example, if I define a function `function1` inside `UtilsFunctions.h` and its source code is in `UtilsFunctions.cxx`, if I try to call it from `MyMasterFile.py` like

```auto
import ROOT
ROOT.gSystem.Load("libPackageName.so")
ROOT.function1()

```

it complains with an attribute error `AttributeError: function1`. This problems doesn’t appear when I call `ClassA` as

```auto
tool = ClassA()
tool.MethodFoo()

```

I also checked that I have this line not commented

```auto
#pragma link C++ defined_in "UtilsFunctions.h";

```

inside `LinkDef.h`

Do I need to add something when calling the function?

Thank you very much for your help! And sorry for these lots of questions

Cheers,  
Francisco

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [March 10, 2021, 4:01pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/8 "2021-03-10T16:01:47Z")

</div>

Hi @fsili,

Indeed, it seems that the functions declared in `UtilsFunctions.h` are not in the generated dictionary. Could you please confirm that these functions are not in a namespace or so? In any case, could you try the following to check if that workarounds the problem?

```auto
#pragma link C++ all function

```

I will invite @pcanal to this thread as he might know what the issue might be. @pcanal, you can skip the whole thread and only read the latest @fsili post.

Cheers,  
Javier.

---

<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:** [March 10, 2021, 7:50pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/9 "2021-03-10T19:50:52Z")

</div>

> [@fsili](#):
>
> `#pragma link C++ defined_in "UtilsFunctions.h";`

If I remember correctly, you need to give the full path name here.

However this should not make a difference for a function.

What command line did you use to generate the dictionary?

---

<div class="post-metadata">

**Author:** ![fsili](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@fsili](https://root-forum.cern.ch/u/fsili)\
**Post date:** [March 10, 2021, 7:59pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/10 "2021-03-10T19:59:02Z")

</div>

Hi!

> [@pcanal](#):
>
> If I remember correctly, you need to give the full path name here.

I changed it and gave the full path and now it works!

> [@jalopezg](#):
>
> `#pragma link C++ all function`

This solution by @jalopezg also worked!

Thank you both very very much for your help! 😃

Cheers,  
Francisco

---

<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:** [March 24, 2021, 7:59pm UTC](https://root-forum.cern.ch/t/shared-library-stopped-working-when-external-functions-were-added/43838/11 "2021-03-24T19:59:11Z")

</div>

Closed on March 24, 2021, 7:59pm UTC

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