Search code examples
c++shared-librariesdlopentensorrt

dlopen works with "libnvinfer.so.8" but not with "libnvinfer.so.8.5.1"


int main(int argc, char** argv) {
  std::string r = ::file::RLocation("tensor_rt/lib");
  setenv("LD_LIBRARY_PATH", r.c_str(), 1);

  std::string filename = "libnvinfer.so.8";
  CHECK(std::filesystem::exists(r+"/"+filename));
  void * handle = dlopen(filename.c_str(), RTLD_NOW | RTLD_LOCAL);
  CHECK(handle);

  filename = "libnvinfer.so.8.5.1";
  CHECK(std::filesystem::exists(r+"/"+filename));
  handle = dlopen(filename.c_str(), RTLD_NOW | RTLD_LOCAL);
  CHECK(handle) << dlerror();
}

This program fails at the last CHECK

F20221212 15:23:07.725180 2924123 test.cc:24] Check failed: handle libnvinfer.so.8.5.1: cannot open shared object file: No such file or directory

Why does it work with the first dlopen but not with the second one? The two files should be the same.

Edit: ls -l gives

lrwxrwxrwx 1 shshao shshao         19 Dec 12 15:20 libnvinfer.so -> libnvinfer.so.8.5.1
lrwxrwxrwx 1 shshao shshao         19 Dec 12 15:20 libnvinfer.so.8 -> libnvinfer.so.8.5.1
-rwxr-xr-x 1 shshao shshao  487512744 Oct 27 15:37 libnvinfer.so.8.5.1

Solution

  • It's because unlike other env variables, LD_LIBRARY_PATH can't be modified within a program: https://groups.google.com/g/comp.lang.java.programmer/c/LOu18-OWAVM/m/b0YBJhoKS04J