Linux下编写C++代码引用opencv头文件,编译报错 undefined reference to `cv::imread的解决办法

Linux下编写C++代码引用opencv头文件,编译报错 undefined reference to `cv::imread(std::__cxx11::basic_string const&, int)的两种解决办法

1. 首先确定已安装opencv,可在终端输入命令查看:
$ opencv_version
3.3.1-dev

或输入以下命令查看opencv库的路径:

$ pkg-config --cflags --libs opencv
-I/usr/include/opencv /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so -lopencv_calib3d /usr/lib/x86_64-linux-gnu/libopencv_contrib.so -lopencv_contrib /usr/lib/x86_64-linux-gnu/libopencv_core.so -lopencv_core /usr/lib/x86_64-linux-gnu/libopencv_features2d.so -lopencv_features2d /usr/lib/x86_64-linux-gnu/libopencv_flann.so -lopencv_flann /usr/lib/x86_64-linux-gnu/libopencv_gpu.so -lopencv_gpu /usr/lib/x86_64-linux-gnu/libopencv_highgui.so -lopencv_highgui /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so -lopencv_imgproc /usr/lib/x86_64-linux-gnu/libopencv_legacy.so -lopencv_legacy /usr/lib/x86_64-linux-gnu/libopencv_ml.so -lopencv_ml /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so -lopencv_objdetect /usr/lib/x86_64-linux-gnu/libopencv_ocl.so -lopencv_ocl /usr/lib/x86_64-linux-gnu/libopencv_photo.so -lopencv_photo /usr/lib/x86_64-linux-gnu/libopencv_stitching.so -lopencv_stitching /usr/lib/x86_64-linux-gnu/libopencv_superres.so -lopencv_superres /usr/lib/x86_64-linux-gnu/libopencv_ts.so -lopencv_ts /usr/lib/x86_64-linux-gnu/libopencv_video.so -lopencv_video /usr/lib/x86_64-linux-gnu/libopencv_videostab.so -lopencv_videostab

如果并没有看到opencv的相关信息,可去opencv官网安装,或参照一位名为Ding_0110M的博客。

2. 用GNU命令编译

命令如下:

// 编译生成可执行文件executableFileName
$ g++ source.cpp -o executableFileName $(pkg-config --cflags --libs opencv)
// 运行可以执行文件
$ ./executableFileName

这里提醒一点:代码中包含opencv头文件时注意格式:#include,不要将 / 误写为 \ 。

3. 使用cmke编译
  1. 在项目中新建CMakeLists.txt文件;
  2. 将以下内容复制粘贴到CMakeLists.txt文件中:
cmake_minimum_required(VERSION 2.2)

project(project)

set(CMAKE_CXX_STANDARD 11)
find_package( OpenCV REQUIRED )
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(executableFileName source.cpp)
target_link_libraries( executableFileName ${OpenCV_LIBS} )
  1. 切换到当前工程所在目录下进行编译:
$ cmake .
$ make
$ ./executableFileName

你可能感兴趣的:(Linux下编写C++代码引用opencv头文件,编译报错 undefined reference to `cv::imread的解决办法)