0
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
Shuai Shao
  • 11
  • 2
  • 1
    And your proof that `libnvinfer.so.8.5.1` is what, exactly? It either exists, and is accessible, or it doesn't exist, or is inaccessible. `errno` is your friend. Are you familiar with `errno`, and know what it is? – Sam Varshavchik Dec 12 '22 at 23:29
  • What is `CHECK`? What does it do? Please try to create a [mre] to show us. – Some programmer dude Dec 12 '22 at 23:31
  • @SamVarshavchik You can see `std::filesystem::exists` can pass for both files. – Shuai Shao Dec 12 '22 at 23:32
  • That doesn't mean that it's accessible. For example I have a symbolic link, right here to `/my/hot/dog/has/a/first/name`. `std::filesystem::exists` will be very happy, but any attempt to open it will not have many chances of success. – Sam Varshavchik Dec 12 '22 at 23:32
  • @Someprogrammerdude an util from glog that crash if the value is false – Shuai Shao Dec 12 '22 at 23:33
  • 1
    On another note, why are you using `setenv("LD_LIBRARY_PATH", r.c_str(), 1)`? Setting `LD_LIBRARY_PATH` is usually not a good idea. And you pass (I assume) full and absolute paths to `dlopen` so setting it should not be needed? – Some programmer dude Dec 12 '22 at 23:36
  • @SamVarshavchik I tested std::fopen can also work. Or is there another suggested way? – Shuai Shao Dec 12 '22 at 23:37
  • Then is it a proper shared-library as needed by [`dlopen`](https://man7.org/linux/man-pages/man3/dlopen.3.html)? There's no other problem that opening it might cause? Can you please go to the directory where the libraries are, and do an `ls -l libnvinfer.*`, and copy-paste the output (as text!) into your question? – Some programmer dude Dec 12 '22 at 23:39
  • Also please [edit] your question to include the details you have told us in the comments, those should be inside the question itself. – Some programmer dude Dec 12 '22 at 23:40
  • The "suggested way" is to actually look at the contents of the directory. – Sam Varshavchik Dec 12 '22 at 23:47
  • Updated the question with ls -l results – Shuai Shao Dec 12 '22 at 23:49

1 Answers1

0

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

Shuai Shao
  • 11
  • 2