Vortico
Vortico

Reputation: 2749

Allow plugins loaded with dlopen() to dynamically link to shared objects in their own directory

I have an application that loads plugins with dlopen(). The plugins themselves might need to dynamically link to another library (say libwhatever.so) which is distributed in their own plugin folder. The plugin paths are not known to the application or plugin until runtime.

Attempts

1) Link plugins with -Wl,-rpath,. This doesn't work because . refers to the application directory, not the plugin directory. It would need to use -Wl,-rpath,relative/path/to/plugin, but this relative path is not known to the plugin at compile time.

2) Use setenv("LD_LIBRARY_PATH", pluginPath, 1) before dlopen() in the application. This doesn't work because ld.so caches LD_LIBRARY_PATH before the application starts and doesn't pay attention to the environment variable during runtime. Maybe there's a way a refresh this cache?

Upvotes: 1

Views: 171

Answers (1)

Employed Russian
Employed Russian

Reputation: 213596

You are looking for:

-Wl,-rpath='$ORIGIN'

(Note: single-quote it -- you don't want shell to be expanding it).

From the man page:

  $ORIGIN (or equivalently ${ORIGIN})
      This expands to the directory containing the program or shared
      object.

Upvotes: 1

Related Questions