Is it possible to build a shared library (with g++) that has a dependency on another shared library (in my case RocksDB - librocksdb.so
), but whose file name at runtime is different? (e.g. librocksdb1234.so
). And if so, how can this be done?
More Context
I'm implementing a custom compaction filter for RocksDB (C++) that I intend to use in my Java application via JNI.
When using RocksDB in Java I need to first invoke
RocksDB.loadLibrary()
, which will load the RocksDB library. The RocksDB JNI jar comes with multiple RocksDB shared objects (.so files), one for each architecture. When I call this method it will select the correct shared object file in the jar, copy it to a temporary folder, and load it usingSystem.load(...)
. The copied shared object file is created with a random suffix.For the development of the custom compaction filter, I first installed RocksDB headers (this is simply a call to a Makefile task in the RocksDB project, that will copy the headers to
/usr/local/include/rocksdb
), and also built RocksDB as a shared library and installed it in/usr/local/lib
(again, this is just a call to a Makefile task that already exists in the project).To build the shared library with the custom compaction filter, I'm using g++ as follows (for linking):
g++ -shared -fPI -Wl,--no-undefined <list-of-object-files> -o libcustom.so -lrocksdb
(the installed RocksDB shared library is namedlibrocksdb.so
).When I try to load my shared library with the custom compaction filter on my Java application it is successfully loaded as expected (as long as the
librocksdb.so
is present).But since RocksDB JNI already comes with the RocksDB shared object files, what I want is to just use those shared objects, and not the
librocksdb.so
that I installed for development purposes. So, I basically just delete thelibrocksdb.so
and add the call toRocksDB.loadLibrary()
in my Java application. Once I do this, I stop being able to load my shared library due tolibrocksdb.so.6: cannot open shared object file: No such file or directory
.I validated that the RocksDB shared object (from the jar) was properly loaded using
lsof -p <pid>
.From what I understand, this happens because my shared object with the custom compaction filter will have an entry in its header that defines a dependency on a library named
rocksdb
(confirmed usingobjdump -x ...
). But because the RocksDB shared object loaded byRocksDB.loadLibrary()
has a random name, it won't match the needed dependency.
Dynamic Section:
NEEDED librocksdb.so.6
...
Side question In point 8. I'm assuming that the dynamic loading will be based on the file name of the shared object. Is this a correct assumption? Or will the dynamic loading be based on the SONAME? (I noticed that the shared objects in the RocksDB JNI jar don't have the SONAME defined).