TensorRT学习笔记–Linux基于VSCode利用CMake编译C++ Sample

  • Post author:
  • Post category:linux



目录


1–前言


2–CMakeLists.txt编写


3–实例



1–前言


博主系统环境如下:


System:Ubuntu 20.04


OpenCV:4.70


Cuda:11.3


Tensor RT:8.2.5.1


2–CMakeLists.txt编写


需要使用OpenCV、TensorRT以及Cuda相应的头文件和动态库,以下提供一个 CMakeLists.txt 编写样例,需根据个人实际修改具体路径:

cmake_minimum_required(VERSION 3.13)
project(TensorRT_test)
set(CMAKE_CXX_STANDARD 11)

set(SAMPLES_COMMON_SOURCES "/home/liujinfu/Downloads/TensorRT-8.2.5.1/samples/common/logger.cpp")
add_executable(TensorRT_test sampleMNIST.cpp ${SAMPLES_COMMON_SOURCES})

# add OpenCV
find_package(OpenCV REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})

# add TensorRT8
include_directories(/home/liujinfu/Downloads/TensorRT-8.2.5.1/include)
include_directories(/home/liujinfu/Downloads/TensorRT-8.2.5.1/samples/common)
set(TENSORRT_LIB_PATH "/home/liujinfu/Downloads/TensorRT-8.2.5.1/lib")
file(GLOB LIBS "${TENSORRT_LIB_PATH}/*.so")

# add CUDA
find_package(CUDA 11.3 REQUIRED)
message("CUDA_LIBRARIES:${CUDA_LIBRARIES}")
message("CUDA_INCLUDE_DIRS:${CUDA_INCLUDE_DIRS}")
include_directories(${CUDA_INCLUDE_DIRS})

# link
target_link_libraries(TensorRT_test ${LIBS} ${CUDA_LIBRARIES} ${OpenCV_LIBS})


3–实例


基于 Tensor RT 8.2.5 提供的 sampleMNIST.cpp 文件,在 VSCode 下利用 CMake 编译成可执行文件:

mkdir build 

cd build

cmake ..

make


编译过程:


运行测试:

./TensorRT_test -d /home/liujinfu/Downloads/TensorRT-8.2.5.1/data/mnist


当输入数字6时,结果识别为6,编译得到的可执行文件运行结果正确。



版权声明:本文为weixin_43863869原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。