简体   繁体   English

即使在链接库时,也会获得对 OpenCV 元素的未定义引用。 使用 CMake

[英]Getting undefined reference to OpenCV elements even when linking libraries. Using CMake

I'm trying to compile this code:我正在尝试编译此代码:

#include <opencv2/opencv.hpp>

using namespace cv;

int main()
{
  Mat image = imread ("tree.jpg", IMREAD_GRAYSCALE);
  imshow ("Grayscale Image", image);
  waitKey (0);
  return 0;
}

But I get undefined references to all the OpenCV elements.但是我得到了对所有 OpenCV 元素的未定义引用。 From reading on the site it seems this happens because of problems with linking the OpenCV libraries.从网站上的阅读来看,这似乎是因为链接 OpenCV 库时出现问题。

Is there a problem with my CMake file?我的 CMake 文件有问题吗?

cmake_minimum_required(VERSION 3.17)
project(Programming)

set(CMAKE_CXX_STANDARD 17)
find_package(OpenCV REQUIRED PATHS C:\\opencv\\build\\x64\\vc15\\lib)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(project main.cpp)
target_link_libraries(project ${OpenCV_LIBS})

MESSAGE("OpenCV_LIBS: " ${OpenCV_LIBS})

The OpenCV libraries seem to be found:似乎找到了 OpenCV 库:

OpenCV_LIBS: opencv_calib3dopencv_coreopencv_dnnopencv_features2dopencv_flannopencv_gapiopencv_highguiopencv_imgcodecsopencv_imgprocopencv_mlopencv_objdetectopencv_photoopencv_stitchingopencv_videoopencv_videoioopencv_world

So I'm not sure the problem is.所以我不确定问题是否存在。

EDIT: This is the complete error message:编辑:这是完整的错误消息:

/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles/project.dir/main.cpp.o:/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/main.cpp:9: undefined reference to `cv::imread(std::string const&, int)'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles/project.dir/main.cpp.o: in function `main':
/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/main.cpp:11: undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/main.cpp:12: undefined reference to `cv::waitKey(int)'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/main.cpp:9: undefined reference to `cv::Mat::~Mat()'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/main.cpp:9: undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/project.dir/build.make:120: project.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:96: CMakeFiles/project.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:103: CMakeFiles/project.dir/rule] Error 2
make: *** [Makefile:138: project] Error 2

The problem is, your openCV libraries are installed in a non standard path, and the names in OpenCV_LIBS do not give you an absolute path.问题是,您的 openCV 库安装在非标准路径中,并且 OpenCV_LIBS 中的名称没有给您绝对路径。 You need to indicate to the linker where to find the libraries.您需要向 linker 指明在哪里可以找到这些库。

To do that, you can use the target_link_directories() cmake command.为此,您可以使用 target_link_directories() cmake 命令。 https://cmake.org/cmake/help/latest/command/target_link_directories.html https://cmake.org/cmake/help/latest/command/target_link_directories.html

I guess the path you want to pass it is "C:\opencv\build\x64\vc15\lib".我猜你要传递的路径是“C:\opencv\build\x64\vc15\lib”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM