Loading...
Searching...
No Matches
eonc::dynlib Namespace Reference

Cross-platform dynamic library loading abstraction. More...

Typedefs

using Handle = void *

Functions

Handle open (const char *name) noexcept
void * sym (Handle h, const char *name) noexcept
void close (Handle h) noexcept
std::string error ()
Handle openFirst (const char *const names[]) noexcept
 Try a list of library names in order, return first successful handle.
template<typename Fn>
Fn loadSym (Handle h, const char *name) noexcept
 Load a symbol and cast to a function pointer type.

Detailed Description

Cross-platform dynamic library loading abstraction.

Used by LammpsLoader, XtbLoader, and any other runtime-loaded potentials.

Typedef Documentation

◆ Handle

using eonc::dynlib::Handle = void *

Definition at line 60 of file DynLib.h.

Function Documentation

◆ close()

void eonc::dynlib::close ( Handle h)
inlinenoexcept

Definition at line 77 of file DynLib.h.

77 {
78 if (h)
79 dlclose(h);
80}

◆ error()

std::string eonc::dynlib::error ( )
inline

Definition at line 82 of file DynLib.h.

82 {
83 const char *msg = dlerror();
84 return msg ? std::string(msg) : std::string{};
85}
STL class.

◆ loadSym()

template<typename Fn>
Fn eonc::dynlib::loadSym ( Handle h,
const char * name )
noexcept

Load a symbol and cast to a function pointer type.

Definition at line 99 of file DynLib.h.

99 {
100 return reinterpret_cast<Fn>(sym(h, name));
101}
void * sym(Handle h, const char *name) noexcept
Definition DynLib.h:75

◆ open()

Handle eonc::dynlib::open ( const char * name)
inlinenoexcept

Definition at line 62 of file DynLib.h.

62 {
63 // RTLD_DEEPBIND (Linux-only): make the library prefer its own symbols over
64 // globally-visible ones. Without this, Fortran helper symbols exported by
65 // Fortran shared libraries (e.g. neighbours_ in libeon_sw.so) can be
66 // hijacked by identically-named symbols in other loaded libraries (e.g.
67 // libxtb.so.6 also exports neighbours_), causing mis-dispatch and SIGSEGV.
68#if defined(__linux__)
69 return dlopen(name, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND);
70#else
71 return dlopen(name, RTLD_NOW | RTLD_LOCAL);
72#endif
73}

◆ openFirst()

Handle eonc::dynlib::openFirst ( const char *const names[])
inlinenoexcept

Try a list of library names in order, return first successful handle.

Definition at line 89 of file DynLib.h.

89 {
90 for (const char *const *name = names; *name; ++name) {
91 Handle h = open(*name);
92 if (h)
93 return h;
94 }
95 return {};
96}
void * Handle
Definition DynLib.h:60
Handle open(const char *name) noexcept
Definition DynLib.h:62

◆ sym()

void * eonc::dynlib::sym ( Handle h,
const char * name )
inlinenoexcept

Definition at line 75 of file DynLib.h.

75{ return dlsym(h, name); }