CUDA——报错解决:编写CUDA+OpenCV混合程序时遇到:undefined reference to `cv::imread(std::__cxx11::basic_string<char,

报错解决:找不到OpenCV头文件或者找到但是报错undefined reference

CUDA——报错解决:编写CUDA+OpenCV混合程序时遇到:undefined reference to `cv::imread(std::__cxx11::basic_string<char,_第1张图片

/tmp/tmpxft_00001e36_00000000-11_main_cuda.o: In function Solver(int, std::__cxx11::basic_string, std::allocator >, std::__cxx11::basic_string, std::allocator >)': tmpxft_00001e36_00000000-6_main_cuda.cudafe1.cpp:(.text+0x209b): undefined reference to cv::imread(std::__cxx11::basic_string const&, int)’
tmpxft_00001e36_00000000-6_main_cuda.cudafe1.cpp:(.text+0x215b): undefined reference to cv::GaussianBlur(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_, double, double, int)' tmpxft_00001e36_00000000-6_main_cuda.cudafe1.cpp:(.text+0x235f): undefined reference to cv::imwrite(std::__cxx11::basic_string const&, cv::_InputArray const&, std::vector const&)’
tmpxft_00001e36_00000000-6_main_cuda.cudafe1.cpp:(.text+0x2766): undefined reference to cv::imwrite(std::__cxx11::basic_string, std::allocator > const&, cv::_InputArray const&, std::vector > const&)' /tmp/tmpxft_00001e36_00000000-11_main_cuda.o: In function main’:
tmpxft_00001e36_00000000-6_main_cuda.cudafe1.cpp:(.text+0x2b91): undefined reference to `cv::imread(std::__cxx11::basic_string const&, int)’

CUDA——报错解决:编写CUDA+OpenCV混合程序时遇到:undefined reference to `cv::imread(std::__cxx11::basic_string<char,_第2张图片

解决方法

-推荐使用方法二,亲测可行。

方法一:将opencv头文件加入系统头文件路径

  • 一般在安装好opencv后,默认的路径都在/usr/local/include/opencv4下,而CUDA程序如果没有使用CMakeList,就没办法手动设置opencv的路径,因此这里需要将opencv的头文件链接到系统头文件中:
sudo ln -s /usr/local/include/opencv4 /usr/include/

CUDA——报错解决:编写CUDA+OpenCV混合程序时遇到:undefined reference to `cv::imread(std::__cxx11::basic_string<char,_第3张图片

方法二:(推荐)

mkdir build && cd build
  • 编写CMakeList,手动链接OpenCV的路径:
cmake_minimum_required(VERSION 3.10)
project(main_cuda)

set(CMAKE_CXX_STANDARD 03)
find_package(CUDA REQUIRED)

# 加载OpenCV 4 的头文件
find_package(OpenCV 4.1.1 REQUIRED) 
include_directories(${OpenCV_INCLUDE_DIRS}) 

cuda_add_executable(main_cuda main_cuda.cu)

target_link_libraries(main_cuda ${OpenCV_LIBS})
  • 编译:
cmake ..
make

CUDA——报错解决:编写CUDA+OpenCV混合程序时遇到:undefined reference to `cv::imread(std::__cxx11::basic_string<char,_第4张图片

参考文章:

  • fatal error: opencv2/opencv.hpp: No such file or directory
  • undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::all

你可能感兴趣的:(opencv,CUDA,opencv,人工智能)