How to statically link TensorRT?

Hi, I have a program like:

// test.cc
#include <iostream>
#include "NvInfer.h"

int main(int argc, char** argv) {
  std::cout << "getInferLibVersion: " << getInferLibVersion() << std::endl;
  return 0;
}

And I compile it like:

g++ -Wall -std=c++11 -I<some_include_paths> -D_REENTRANT -c -o /tmp/test.o test.cc

and dynamically link it like:

g++ -o /tmp/test /tmp/test.o -L<some_library_paths> -Wl,--start-group -lnvinfer -lrt -ldl -lpthread -Wl,--end-group

and it works well. Running it gives:

getInferLibVersion: 3001

For some reasons I need to link TensorRT statically, I tried:

g++ -o /tmp/test /tmp/test.o -L<some_library_paths> -Wl,-Bstatic -lnvinfer -Wl,-Bdynamic -lrt -ldl -lpthread -Wl,--as-needed

but got

/usr/bin/ld: <some_library_path>/libnvinfer.a(Infer.o): relocation R_X86_64_32S against symbol `_ZTVN8nvinfer17BuilderE' c
an not be used when making a shared object; recompile with -fPIC

I also trid a few different approaches but non of them works. Can someone help? What is the correct command to statically link the TensorRT library?

Btw I installed TensorRT via TensorRT-3.0.1.Ubuntu-16.04.3.x86_64.cuda-9.0.cudnn7.0.tar.gz.
Thanks.

1 Like

I think the problem is g++, as when I switch to clang++ the error is gone.
My g++ version is g++ (Debian 6.3.0-18) 6.3.0 20170516.