Error Code 6: Internal Error (Unable to load library: libnvinfer_builder_resource.so.8.6.1

ubuntu22 tensorrt 报错Error Code 6: Internal Error (Unable to load library: libnvinfer_builder_resourc

上述参考文章的解决方案在我的环境中(ubuntu22.04 clion)有效。

sudo cp libnvinfer_builder_resource.so.8.6.1 /usr/lib/

并且通过建立软链接也是无效的,它会报libnvinfer_builder_resource.so.8.6.1 too many 符号链接的错误。

# 尝试过软链接的方法,发现无效
ln -s libnvinfer_builder_resource.so.8.6.1 /usr/lib/libnvinfer_builder_resource.so

下面仅谈一谈我自己的思考

完整报错:

Error Code 6: Internal Error (Unable to load library: libnvinfer_builder_resource.so.8.6.1:libnvinfer_builder_resource.so.8.5.1: cannot open shared object file: No such file or directory

代码在编译的时候未报错,而在运行时被抛出。 

首先,这个报错是在使用tensorRT的builder的时候抛出。

// =========== 1. 创建builder ===========
    auto builder = std::unique_ptr(nvinfer1::createInferBuilder(sample::gLogger.getTRTLogger()));
    if (!builder)
    {
        std::cerr << "Failed to create builder" << std::endl;
        return -1;
    }

其次,我在运行代码的时候,的确已经将TensorRT的lib添加到LD_LIBRARY_PATH,但这样运行时仍无法找到。

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-12.0/lib64:/opt/TensorRT-8.6.1.6/lib

然后,我去可执行文件中查看RunPath

readelf -d Density

Dynamic section at offset 0xc95a0 contains 92 entries:
  标记        类型                         名称/值
 0x0000000000000001 (NEEDED)             共享库:[libyolo_plugin.so]
 0x0000000000000001 (NEEDED)             共享库:[libyolo_utils.so]
 0x0000000000000001 (NEEDED)             共享库:[libnvinfer.so.8]
 0x0000000000000001 (NEEDED)             共享库:[libnvinfer_plugin.so.8]
 0x0000000000000001 (NEEDED)             共享库:[libnvparsers.so.8]
 0x0000000000000001 (NEEDED)             共享库:[libnvonnxparser.so.8]
 0x0000000000000001 (NEEDED)             共享库:[libopencv_stitching.so.4.5d]

....
 0x0000000000000001 (NEEDED)             共享库:[libstdc++.so.6]
 0x0000000000000001 (NEEDED)             共享库:[libm.so.6]
 0x0000000000000001 (NEEDED)             共享库:[libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             共享库:[libc.so.6]
 0x0000000000000001 (NEEDED)             共享库:[ld-linux-x86-64.so.2]
 0x000000000000001d (RUNPATH)            Library runpath: [/home/buntu/gitRepository/axx/cmake-build-debug:/opt/TensorRT-8.6.1.6/lib]
 0x000000000000000c (INIT)               0xd000
 0x000000000000000d (FINI)               0x91cd4
 0x0000000000000019 (INIT_ARRAY)         0xc6000
 0x000000000000001b (INIT_ARRAYSZ)       56 (bytes)
 0x000000000000001a (FINI_ARRAY)         0xc6038
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x3d8
 0x0000000000000005 (STRTAB)             0x1ea8
 0x0000000000000006 (SYMTAB)             0x4b0
 0x000000000000000a (STRSZ)              8873 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0xcaba0
 0x0000000000000002 (PLTRELSZ)           5400 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0xb2f8
 0x0000000000000007 (RELA)               0x4500
 0x0000000000000008 (RELASZ)             28152 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000000000001e (FLAGS)              BIND_NOW
 0x000000006ffffffb (FLAGS_1)            标志: NOW PIE
 0x000000006ffffffe (VERNEED)            0x4380
 0x000000006fffffff (VERNEEDNUM)         5
 0x000000006ffffff0 (VERSYM)             0x4152
 0x000000006ffffff9 (RELACOUNT)          1127
 0x0000000000000000 (NULL)               0x0

 可以看到这个路径是包含在其中的。在一定程度上说明不是LD_LIBRARY_PATH的问题。

将LD_LIBRARY_PATH添加在`~/.bashrc`,我怀疑可能有问题,所以,我在/etc/ld.so.conf.d同样做了添加

sudo touch TensorRT-8_6.conf

vim TensorRT-8_6.conf
/opt/TensorRT-8.6.1.6/lib

sudo ldconfig

ldconfig -p | grep nvinfer_builder_resource

do_not_link_against_nvinfer_builder_resource (libc6,x86-64) => /opt/TensorRT-8.6.1.6/lib/do_not_link_against_nvinfer_builder_resource
# 这里很让我感到奇怪,为什么是do_not_link_against_nvinfer_builder_resource

但出现了do_not_link_against_nvinfer_builder_resource,于是参考了第二篇文章

c++ Ldd显示了一个奇怪的库“do_not_link_against_nvinfer_builder_resource

里面解释到

这是因为TensorRT的作者使用了一种黑客技术,在libnvinfer_builder_resource.so.8.5.1中添加了一个do_not_link_against_nvinfer_builder_resource。我们可以通过readelf -d libnvinfer_builder_resource.so.8.5.1来证实这一点 

readelf -d libnvinfer_builder_resource.so.8.6.1

Dynamic section at offset 0x1d18 contains 31 entries:
  标记        类型                         名称/值
 0x0000000000000003 (PLTGOT)             0x2fe8
 0x0000000000000002 (PLTRELSZ)           216 (bytes)
 0x0000000000000017 (JMPREL)             0x810
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000007 (RELA)               0x5e8
 0x0000000000000008 (RELASZ)             552 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffff9 (RELACOUNT)          18
 0x0000000000000006 (SYMTAB)             0x228
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000005 (STRTAB)             0x378
 0x000000000000000a (STRSZ)              385 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x500
 0x0000000000000001 (NEEDED)             共享库:[libstdc++.so.6]
 0x0000000000000001 (NEEDED)             共享库:[libm.so.6]
 0x0000000000000001 (NEEDED)             共享库:[libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             共享库:[libc.so.6]
 0x000000000000000e (SONAME)             Library soname: [do_not_link_against_nvinfer_builder_resource]
 0x000000000000000c (INIT)               0x8e8
 0x000000000000000d (FINI)               0xce8
 0x000000000000001a (FINI_ARRAY)         0x2d00
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x0000000000000019 (INIT_ARRAY)         0x2d08
 0x000000000000001b (INIT_ARRAYSZ)       16 (bytes)
 0x000000006ffffffb (FLAGS_1)            标志: NODUMP
 0x000000006ffffff0 (VERSYM)             0x530
 0x000000006ffffffc (VERDEF)             0x54c
 0x000000006ffffffd (VERDEFNUM)          1
 0x000000006ffffffe (VERNEED)            0x568
 0x000000006fffffff (VERNEEDNUM)         3
 0x0000000000000000 (NULL)               0x0

它的Library soname竟然是一个这玩意: [do_not_link_against_nvinfer_builder_resource] ,

并且我去TensorRT的lib下看到这样一个东西

cd /opt/TensorRT-8.6.1.6/lib

ll

lrwxrwxrwx 1 buntu buntu         36  1月  2 11:38 do_not_link_against_nvinfer_builder_resource -> libnvinfer_builder_resource.so.8.6.1*

-rwxr-xr-x 1 buntu buntu 1003289256  4月 27  2023 libnvinfer_builder_resource.so.8.6.1*

里面有一个软链接do_not_link_against_nvinfer_builder_resource指向的是nvinfer_builder_resource,很奇怪。

这个错误Unable to load library: libnvinfer_builder_resource.so.8.6.1,我想可能是这个do_not_link造成的,但我无法证明。

希望有知道的伙伴在评论区指点一二

其它参考链接:

记录TensorRT使用时发现的问题和解决方法

https://github.com/wang-xinyu/tensorrtx/issues/1205

TensorRT 8.4.2 · Issue #2218 · NVIDIA/TensorRT · GitHub

你可能感兴趣的:(c++,c++,TensorRT)