# Compiling dynamic library with ACLIC using "-rpath"

**URL:** <https://root-forum.cern.ch/t/compiling-dynamic-library-with-aclic-using-rpath/63214>\
**Category:** Cling\
**Tags:** root\
**Created:** [March 25, 2025, 4:29pm UTC](https://root-forum.cern.ch/t/compiling-dynamic-library-with-aclic-using-rpath/63214 "2025-03-25T16:29:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [March 25, 2025, 4:29pm UTC](https://root-forum.cern.ch/t/compiling-dynamic-library-with-aclic-using-rpath/63214/1 "2025-03-25T16:29:19Z")

</div>

Hello.

I want to compile dynamic library with ACLIC and link it it with another library compiled by gcc. And then use this library in CLING. How can i do that?

That’s what I have:

“ns\_lib.hpp”:

```auto
#ifndef _NS_LIB_HPP_
# define _NS_LIB_HPP_

extern char const *gBuf;

extern int main_ns_lib();

#endif

```

“ns\_lib.cpp”:

```auto
#ifndef _NS_LIB_CPP_
# define _NS_LIB_CPP_

# include "ns_lib.hpp"

# include <stdio.h>

# include "NS/FuncUtils.hxx"
# include "NS/CommonUtils.hxx"

char const *gBuf = "test buffer";

int main_ns_lib()
{
	NS_abs_buf buf;
	double a[] = {0, 1, 2};

	NS_AbsBufInit(&buf);

	printf("Hello world from main_ns_lib()!\n");
	printf("Check FuncUtils: pol = %f\n", NS_PolF(a, 2, 1));

	NS_AbsBufPushBack< double >(&buf, 2);
	NS_AbsBufFini(&buf);
	return (0);
}
#endif

```

where `NS_abs_buf, NS_AbsBufInit, NS_AbsBufPushBack, NS_AbsBufFini` are from my `CommonUtils` dynamic library, and `NS_PolF` is from `FuncUtils` dynamic library.

So, i try to compile the library with this script:

```auto
#include <string.h>

#include "root/TSystem.h"

void build_aclic(char const *aLib)
{
	char strb[128];

	gSystem->AddIncludePath("-I${TESTDIR}/src/include"); // includedir of "ns_lib.hpp"
	sprintf(strb, "./src/test/root6/%s", aLib);
	printf("compile 'ns_lib.cpp ...\n");
	gSystem->AddLinkedLibs("-L/usr/local/lib/NSutils -lFuncUtils -lCommonUtils");
	gSystem->CompileMacro(strb, "kfcv-", "ns_lib", "./build/aclic");
	return;
}

```

The library “ns\_lib.so” is created successfully.  
Now I want to use this library in another script (“load\_lib.cpp”):

```auto
#include "root/Rtypes.h"
#include "root/TROOT.h"

R__ADD_INCLUDE_PATH($PROJECTSDIR/test.world/src/include) // includedir of ns_lib.hpp and ns_lib2.hpp
#include "ns_lib.hpp"
#include "ns_lib2.hpp"

R__ADD_LIBRARY_PATH($PROJECTSDIR/test.world/build/aclic) // path of ns_lib.so

R__LOAD_LIBRARY(ns_lib.so)

int main_load_aclic_libs()
{
	printf("libraries: %s\n", gSystem->GetLibraries());
	main_ns_lib();
	return (0);
}

```

But after loading this script in CLING (in ROOT session) with `.L load_lib.cpp` I have the next:

```auto
cling::DynamicLibraryManager::loadLibrary(): libCommonUtils.so: cannot open shared object file: No such file or directory
Error in <AutoloadLibraryMU>: Failed to load library /home/nazar_s/devel/projects/test.world/build/aclic/ns_lib.socling JIT session error: Failed to materialize symbols: { (main, { _Z11main_ns_libv }) }
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _Z20main_load_aclic_libsi, __stmts__ 0, _GLOBAL __sub_I_cling_module_10,__ orc_init_func.cling-module-10, $.cling-module-10.__inits.0 }) }

```

So, what I’m doing wrong?

Best wishes,  
Nazar

---

<div class="post-metadata">

**Author:** ![nazar\_sokour](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nazar_sokour/32/23991_2.png) [@nazar\_sokour](https://root-forum.cern.ch/u/nazar_sokour)\
**Post date:** [March 25, 2025, 6:53pm UTC](https://root-forum.cern.ch/t/compiling-dynamic-library-with-aclic-using-rpath/63214/2 "2025-03-25T18:53:09Z")

</div>

Ok, I found what I need.  
I should manually add `-Wl,-rpath=/usr/local/lib/NSutils` to `fMakeSharedLib` using `gSystem->SetMakeSharedLib()`.

So:

1. I get the `fMakeSharedLib` with `gSystem->GetMakeSharedLib()`
2. Insert `-Wl,-rpath=/my/path` right before `"$LinkedLibs"`
3. Push it back with `gSystem->SetMakeSharedLib()`
4. Compiling by `gSystem->CompileMacro(strb, "kfcv-", "ns_lib", "./build/aclic");`

Now i can use my `ns_lib.so` in “run-time” without adding library path and loading `CommonUtils` and `FuncUtils`.

---

<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:** [March 25, 2025, 7:04pm UTC](https://root-forum.cern.ch/t/compiling-dynamic-library-with-aclic-using-rpath/63214/3 "2025-03-25T19:04:15Z")

</div>

Thanks for sharing the solution with the ROOT Community!

D

---

<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 8, 2025, 7:04pm UTC](https://root-forum.cern.ch/t/compiling-dynamic-library-with-aclic-using-rpath/63214/4 "2025-04-08T19:04:17Z")

</div>

Closed on April 8, 2025, 7:04pm UTC

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