编译环境
系统: centos7
opencv: 4.1.2
tensorrt: 7.2.2.3
cuda: 10.2
cudnn: 8.0.5
编译前准备
CMakeLists.txt文件需要添加如下行
Tensorrt的头文件
include_directories(/usr/local/TensorRT-7.2.2.3/include)
opencv的头文件
include_directories(/opencv-4.1.2/install/include/opencv4)
Tensorrt的lib文件
link_directories(/usr/local/TensorRT-7.2.2.3/targets/x86_64-linux-gnu/lib/)
添加环境变量
export OpenCV_DIR=/opencv-4.1.2/install
正式编译
进入源码目录
mkdir build
cmake …
make
编译中的问题
问题1
CMake Error at CMakeLists.txt:18 (find_package): By not providing
“FindOpenCV.cmake” in CMAKE_MODULE_PATH this project has asked CMake
to find a package configuration file provided by “OpenCV”, but CMake
did not find one.Could not find a package configuration file provided by “OpenCV”
(requested version 4.1.2) with any of the following names:OpenCVConfig.cmake opencv-config.cmake
Add the installation prefix of “OpenCV” to CMAKE_PREFIX_PATH or set
“OpenCV_DIR” to a directory containing one of the above files. If
“OpenCV” provides a separate development package or SDK, be sure it
has been installed.– Configuring incomplete, errors occurred! See also “/home/tensorrt/yolo-tensorrt/build/CMakeFiles/CMakeOutput.log”. See
also “/home/tensorrt/yolo-tensorrt/build/CMakeFiles/CMakeError.log”.
解决
将opencv的安装目录加入环境变量 OpenCV_DIR
问题2
[ 95%] Building CXX object
CMakeFiles/yolo-trt.dir/samples/sample_detector.cpp.o [100%] Linking
CXX executable yolo-trt
CMakeFiles/yolo-trt.dir/samples/sample_detector.cpp.o: In function
cv::Mat::~Mat()': sample_detector.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x89): undefined reference to
cv::Mat::deallocate()’
sample_detector.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x72):
undefined reference tocv::fastFree(void*)' CMakeFiles/yolo-trt.dir/samples/sample_detector.cpp.o: In function
cv::Mat::clone() const’:
sample_detector.cpp:(.text._ZNK2cv3Mat5cloneEv[_ZNK2cv3Mat5cloneEv]+0x84):
undefined reference tocv::Mat::copyTo(cv::_OutputArray const&) const' CMakeFiles/yolo-trt.dir/samples/sample_detector.cpp.o: In function
std::vector<cv::Mat, std::allocatorcv::Mat >::~vector()’:
sample_detector.cpp:(.text._ZNSt6vectorIN2cv3MatESaIS1_EED2Ev[_ZNSt6vectorIN2cv3MatESaIS1_EED5Ev]+0xc1):
undefined reference tocv::fastFree(void*)' sample_detector.cpp:(.text._ZNSt6vectorIN2cv3MatESaIS1_EED2Ev[_ZNSt6vectorIN2cv3MatESaIS1_EED5Ev]+0x10c): undefined reference to
cv::Mat::deallocate()’
CMakeFiles/yolo-trt.dir/samples/sample_detector.cpp.o: In function
`void std::vector<cv::Mat, std::allocatorcv::Mat::_M_realloc_insert<cv::Mat const&>(__gnu_cxx::_normal_iterator<cv::Mat*, std::vector<cv::Mat,
std::allocatorcv::Mat > >, cv::Mat const&)’:
sample_detector.cpp:(.text.ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT[ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT]+0x1bc):
undefined reference tocv::Mat::copySize(cv::Mat const&)' sample_detector.cpp:(.text._ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT_[_ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT_]+0x2a7): undefined reference to
cv::Mat::copySize(cv::Mat const&)’
sample_detector.cpp:(.text.ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT[ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT]+0x384):
undefined reference tocv::fastFree(void*)' sample_detector.cpp:(.text._ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT_[_ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT_]+0x3e4): undefined reference to
cv::Mat::deallocate()’
sample_detector.cpp:(.text.ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT[ZNSt6vectorIN2cv3MatESaIS1_EE17_M_realloc_insertIJRKS1_EEEvN9__gnu_cxx17__normal_iteratorIPS1_S3_EEDpOT]+0x417):
undefined reference tocv::Mat::copySize(cv::Mat const&)' CMakeFiles/yolo-trt.dir/samples/sample_detector.cpp.o: In function
main’: sample_detector.cpp:(.text.startup+0x13c): undefined reference
tocv::imread(std::string const&, int)' sample_detector.cpp:(.text.startup+0x184): undefined reference to
cv::imread(std::string const&, int)’
sample_detector.cpp:(.text.startup+0x5be): undefined reference to
cv::putText(cv::_InputOutputArray const&, std::string const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)' sample_detector.cpp:(.text.startup+0x87b): undefined reference to
cv::rectangle(cv::InputOutputArray const&, cv::Rect,
cv::Scalar const&, int, int, int)’
sample_detector.cpp:(.text.startup+0xbfb): undefined reference to
cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)' sample_detector.cpp:(.text.startup+0xca2): undefined reference to
cv::waitKey(int)’ sample_detector.cpp:(.text.startup+0x100c):
undefined reference tocv::Mat::copySize(cv::Mat const&)' libdetector.so: undefined reference to
cv::copyMakeBorder(cv::_InputArray const&, cv::OutputArray const&,
int, int, int, int, int, cv::Scalar const&)’ libdetector.so:
undefined reference tocv::Mat::create(int, int const*, int)' libdetector.so: undefined reference to
cv::resize(cv::_InputArray
const&, cv::OutputArray const&, cv::Size, double, double, int)’
libdetector.so: undefined reference tocv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)' libdetector.so: undefined reference to
cv::imshow(std::string const&, cv::_InputArray const&)’
libdetector.so: undefined reference to
cv::dnn::dnn4_v20190902::blobFromImages(cv::_InputArray const&, double, cv::Size_<int>, cv::Scalar_<double> const&, bool, bool, int)' libdetector.so: undefined reference to
cv::getTextSize(std::string
const&, int, double, int, int*)’ libdetector.so: undefined reference
to `cv::namedWindow(std::string const&, int)’ collect2: error: ld
returned 1 exit status make[2]: *** [yolo-trt] Error 1 make[1]: ***
[CMakeFiles/yolo-trt.dir/all] Error 2 make: *** [all] Error 2
解决
将set(OpenCV_DIR /opencv-4.1.2)屏蔽
使用 find_package(OpenCV 4.1.2 REQUIRED) 其中4.1.2为opencv满足的最低版本