# Loading a library from a script

**URL:** <https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306>\
**Category:** ROOT\
**Created:** [April 3, 2017, 10:21pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306 "2017-04-03T22:21:03Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 3, 2017, 10:21pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/1 "2017-04-03T22:21:03Z")

</div>

Hi,

Continuing along the lines of extending ROOT functionality with external libraries I have run into another issue that I am hoping someone here can help resolve. If I make a library containing a class `foo` and write a script `bar1.C` that calls the the class:

```auto
void bar1() {
   foo f;
}

```

I can call this from root with no issues as follows:

```auto
[0] .L libFoo.dylib
[1] .x bar1.C

```

If I then try to have the script `bar2.C` do the library loading using `gSystem::Load` as follows:

```auto
void bar2() {'
   gSystem->Load("libFoo.dylib");
   foo f;
}

```

I get the following error message:

```auto
bar2.C:3:4: error: unknown type name 'foo'

```

I can resolve this by adding the appropriate include to `bar3.C`

```auto
#include "foo.h"
void bar2() {'
   gSystem->Load("libFoo.dylib");
   foo f;
}

```

but now receive the following error message:

```auto
IncrementalExecutor::executeFunction: symbol '_ZN11fooC1Ev' unresolved while linking [cling interface function]!
You are probably missing the definition of foo::foo()
Maybe you need to load the corresponding shared library?

```

Any ideas? What is the ‘proper’ way to load an external library for use like this?

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [April 4, 2017, 6:13am UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/2 "2017-04-04T06:13:09Z")

</div>

Hi,

As the first step of interpreting, cling “compiles” that script before running it. For that it cannot resolve the `foo` constructor. I.e. things are out of sequence. Simply use

```cpp
R__ADD_LIBRARY_PATH($FOODIR) // if needed
R__LOAD_LIBRARY(libFoo.dylib)

```

right after the `#include` and it should work.

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 4, 2017, 6:15am UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/3 "2017-04-04T06:15:56Z")

</div>

Hi,

the interpreter of ROOT6 is based on a real compiler and, as such, it looks for all symbols before running the code. So it is somehow “too late” to load the library at runtime since the symbols inside it are needed at compile time.  
Now, there are several ways out. The first one, which is the one I recommend, is to use rootmpafiles. These allow ROOT to autoload the libraries needed for running the code and to parse the necessary headers. Creating a rootmapfile is very easy: just use the -rmf switch or --rootmap in rootcling or genreflex respectively.  
In case libFoo is a library you did not build and you want to use it interactively I think the best option is to

1. Include the header (as you correctly did)
2. Use the R\_\_LOAD\_LIBRARY(libFoo.dylib) macro: this will trigger the loading of the library _at parsing time_, therefore in time for letting cling finding the symbols necessary for the compilation

In ROOT5 your code was working since the interpreter, CINT, was interpreting the code line by line.

Cheers

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 4, 2017, 6:17am UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/4 "2017-04-04T06:17:30Z")

</div>

Hi,

did not see the post which appeared while I was writing 🙂 @Axel is of course right. This is what I describe in my points 1) and 2).

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 4, 2017, 5:06pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/5 "2017-04-04T17:06:37Z")

</div>

> [@Axel](#):
>
> R\_\_ADD\_LIBRARY\_PATH($FOODIR) // if needed  
> R\_\_LOAD\_LIBRARY(libFoo.dylib)

This worked nicely. The only downside was that the entire absolute path needed to be added. Does ROOT following environment variable for where to find headers and libraries? Something similar to gcc’s `INCLUDE_PATH`, `CPLUS_INCLUDE_PATH` and `LIBRARY_PATH`?

> [@Danilo](#):
>
> The first one, which is the one I recommend, is to use rootmpafiles. These allow ROOT to autoload the libraries needed for running the code and to parse the necessary headers. Creating a rootmapfile is very easy: just use the -rmf switch or --rootmap in rootcling or genreflex respectively.

I am generating the rootmap files as I am using the cmake macro `ROOT_GENERATE_LIBRARY` (which I have an issue with, but will not discuss here). I’m not sure how to use these files, it seems they need to be placed somewhere in particular? Remember I am trying to develop this for others to use so the idea is that a cmake file would properly place all the files in the correct places and the user would have to do very little to get it working.

Thanks for the responses so far, they are helpful.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 4, 2017, 6:13pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/6 "2017-04-04T18:13:18Z")

</div>

Also, what do I need to do so that I can recall the same macro multiple times? Currently it crashes with warning of ‘unknown types’ when calling it more than once:

```auto
[0] .x macro.C
Success!
[1] .x macro.C
... error: unknown type name 'foo' ...

```

EDIT: I tried wrapping the library loading in a header guard, but that didn’t resolve the issue.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 5, 2017, 4:56am UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/7 "2017-04-05T04:56:23Z")

</div>

Hi,

> [@ksmith](#):
>
> I’m not sure how to use these files, it seems they need to be placed somewhere in particular?

They need to be in the LD\_LIBRARY\_PATH. Usually the best place is right next to the libraries as we do in ROOT or as the LHC experiments do.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 5, 2017, 4:56am UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/8 "2017-04-05T04:56:58Z")

</div>

Hi,

this looks like a different question related to unloading: do you mind opening another thread about it ?

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 5, 2017, 12:18pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/9 "2017-04-05T12:18:50Z")

</div>

> [@Danilo](#):
>
> They need to be in the LD\_LIBRARY\_PATH. Usually the best place is right next to the libraries as we do in ROOT or as the LHC experiments do.

This won’t work as many of the users are on Mac OS X 10.12 and have System Integrity Protection enabled ([ROOT Dynamic Library Path - #5 by ksmith](https://root-forum.cern.ch/t/root-dynamic-library-path/24305/5))

> [@Danilo](#):
>
> this looks like a different question related to unloading: do you mind opening another thread about it ?

I’d be happy to.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 5, 2017, 2:35pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/10 "2017-04-05T14:35:05Z")

</div>

> [@Danilo](#):
>
> this looks like a different question related to unloading: do you mind opening another thread about it ?

I opened a thread at [Unloading? a library or loading a library again?](https://root-forum.cern.ch/t/unloading-a-library-or-loading-a-library-again/24330)

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 5, 2017, 6:34pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/11 "2017-04-05T18:34:56Z")

</div>

Hi,

it is enough to let the rootmaps sit in the same directory of the libraries. The SIP is a detail.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 5, 2017, 7:38pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/12 "2017-04-05T19:38:16Z")

</div>

thanks for starting another thread!

---

<div class="post-metadata">

**Author:** ![musinsky](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/musinsky/32/11106_2.png) [@musinsky](https://root-forum.cern.ch/u/musinsky)\
**Post date:** [April 7, 2017, 3:23pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/13 "2017-04-07T15:23:37Z")

</div>

> [@Danilo](#):
>
> The first one, which is the one **I recommend, is to use rootmpafiles**.

> [@Generating rootmap files with root 6.04.18](https://root-forum.cern.ch/t/generating-rootmap-files-with-root-6-04-18/21493/2):
>
> For each dependent library you want to specify add the option:
> 
> -rml Rootmap library name  
> Specify the name of the library which contains the autoload keys. This  
> switch can be specified multiple times to autoload several libraries in  
> presence of a particular key.
> 
> Note that we actually **strongly recommend to no longer use this feature** but instead rely on explicitly linking the dependent library to the library itself.

Use or not to use rootmap files ?

Thanks,  
Jan

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 7, 2017, 3:30pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/14 "2017-04-07T15:30:02Z")

</div>

My problem here is that I am using CMake and `ROOT_GENERATE_DICTIONARY`. This macro builds a rootmapfile, but it doesn’t work correctly.

First, it needs to be manually installed, more of an annoyance than anything else:

```auto
#Install the ROOT 6 PCM and rootmap files.
if (${ROOT_VERSION} VERSION_GREATER "6.0")                                                             
   install(                                                                                            
      FILES
         ${CMAKE_CURRENT_BINARY_DIR}/libFooDict_rdict.pcm                                      
         ${CMAKE_CURRENT_BINARY_DIR}/libFooDict.rootmap                                        
      DESTINATION lib/)
endif (${ROOT_VERSION} VERSION_GREATER "6.0")

```

Secondly the rootmap file is incorrectly named for the library `libFooDict.rootmap` and top of the rootmap file reads:

```auto
[libFooDict.so]

```

This is incorrect as I am on a Mac and the extension should by `.dylib` and in addition it should not have the `Dict`. At some point I will make a small example and open a new thread with issues related to `ROOT_GENERATE_DICTIONARY` (I have a couple additional issues as well).

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 7, 2017, 3:32pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/15 "2017-04-07T15:32:50Z")

</div>

Hi Jan,

both Philippe and I are right. If you are running a macro, you will need to load the libraries so better to rely on autoloading and therefore rootmaps. If you build executables or shared objects, better to directly link.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [April 8, 2017, 6:25am UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/16 "2017-04-08T06:25:19Z")

</div>

Hi Jan,

Plus, the first library should be loaded with the help of a rootmap file, all dependent libraries should be loaded by the dynamic linker because they were explicitly linked against the first library.

Cheers, Axel

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [April 8, 2017, 6:30am UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/17 "2017-04-08T06:30:34Z")

</div>

Hi @ksmith,

Yes please open a new thread or even a bug report on the issues with the CMake macro.

Note that MacOS uses .so as shared library extension since 10.7 or 10.6 if I remember correctly. No need to keep .dylib on your side.

But of course the rootmap file should still respect parameters like the library name, if specified.

Cheers, Axel

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 10, 2017, 3:08pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/18 "2017-04-10T15:08:36Z")

</div>

I’ve started to open new thread about CMake issues. I’ll update this post with links to those threads as they are created:

- [ROOT\_GENERATE\_DICTIONARY Include Paths](https://root-forum.cern.ch/t/cmake-root-generate-dictionary-include-path/24381)
- [ROOT\_GENERATE\_DICTIONARY rootmap](https://root-forum.cern.ch/t/cmake-root-generate-dictionary-rootmap/24382)

---

<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:** [April 24, 2017, 3:08pm UTC](https://root-forum.cern.ch/t/loading-a-library-from-a-script/24306/19 "2017-04-24T15:08:37Z")

</div>

Closed on April 24, 2017, 3:08pm UTC

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