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

Posted

技术标签:

【中文标题】即使在链接库时,也会获得对 OpenCV 元素的未定义引用。使用 CMake【英文标题】:Getting undefined reference to OpenCV elements even when linking libraries. Using CMake 【发布时间】:2022-01-04 04:13:24 【问题描述】:

我正在尝试编译这段代码:

#include <opencv2/opencv.hpp>

using namespace cv;

int main()

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

但是我得到了对所有 OpenCV 元素的未定义引用。从网站上的阅读来看,这似乎是因为链接 OpenCV 库时出现问题。

我的 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)

似乎找到了 OpenCV 库:

OpenCV_LIBS: opencv_calib3dopencv_coreopencv_dnnopencv_features2dopencv_flannopencv_gapiopencv_highguiopencv_imgcodecsopencv_imgprocopencv_mlopencv_objdetectopencv_photoopencv_stitchingopencv_videoopencv_videoioopencv_world

所以我不确定问题出在哪里。

编辑:这是完整的错误信息:

====================[ Build | project | Debug ]=================================
C:\Users\adibo\AppData\Local\JetBrains\CLion2020.3\cygwin_cmake\bin\cmake.exe --build /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug --target project -- -j 3
/cygdrive/c/Users/adibo/AppData/Local/JetBrains/CLion2020.3/cygwin_cmake/bin/cmake.exe -S/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming -B/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/make  -f CMakeFiles/Makefile2 project
make[1]: Entering directory '/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug'
/cygdrive/c/Users/adibo/AppData/Local/JetBrains/CLion2020.3/cygwin_cmake/bin/cmake.exe -S/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming -B/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/cygdrive/c/Users/adibo/AppData/Local/JetBrains/CLion2020.3/cygwin_cmake/bin/cmake.exe -E cmake_progress_start /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug/CMakeFiles 2
/usr/bin/make  -f CMakeFiles/Makefile2 CMakeFiles/project.dir/all
make[2]: Entering directory '/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug'
/usr/bin/make  -f CMakeFiles/project.dir/build.make CMakeFiles/project.dir/depend
make[3]: Entering directory '/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug'
cd /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug && /cygdrive/c/Users/adibo/AppData/Local/JetBrains/CLion2020.3/cygwin_cmake/bin/cmake.exe -E cmake_depends "Unix Makefiles" /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug/CMakeFiles/project.dir/DependInfo.cmake --color=
make[3]: Leaving directory '/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug'
/usr/bin/make  -f CMakeFiles/project.dir/build.make CMakeFiles/project.dir/build
make[3]: Entering directory '/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug'
[ 50%] Building CXX object CMakeFiles/project.dir/main.cpp.o
/usr/bin/c++.exe   -isystem /cygdrive/c/opencv/build/include  -g   -std=gnu++17 -o CMakeFiles/project.dir/main.cpp.o -c /cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/main.cpp
[100%] Linking CXX executable project.exe
/cygdrive/c/Users/adibo/AppData/Local/JetBrains/CLion2020.3/cygwin_cmake/bin/cmake.exe -E cmake_link_script CMakeFiles/project.dir/link.txt --verbose=1
/usr/bin/c++.exe -g  -Wl,--enable-auto-import CMakeFiles/project.dir/main.cpp.o  -o project.exe -Wl,--out-implib,libproject.dll.a -Wl,--major-image-version,0,--minor-image-version,0   -LC:/opencv/build/x64/vc15/lib  /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib /cygdrive/c/opencv/build/x64/vc15/lib/opencv_world454d.lib 
/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:123: project.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:99: CMakeFiles/project.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:106: CMakeFiles/project.dir/rule] Error 2
make: *** [Makefile:141: project] Error 2
make[3]: Leaving directory '/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug'
make[2]: Leaving directory '/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug'
make[1]: Leaving directory '/cygdrive/c/Users/adibo/Programming/CLionProjects/Programming/cmake-build-debug'

【问题讨论】:

可以添加完整的错误信息吗? 是的,我已将其添加到帖子中 set(CMAKE_VERBOSE_MAKEFILE ON) :将此添加到您的 cmake 文件并添加完整的输出。 好的,我添加了完整的输出 好的,显然所有库都重定向到 opencv 世界,这是主模块。如果所有必需的符号都存在于这个库中,你能用 nm 命令检查吗? (例如,cygwin 命令行中的“nm opencv_world454d.lib | grep imread”) 【参考方案1】:

问题是,您的 openCV 库安装在非标准路径中,并且 OpenCV_LIBS 中的名称没有给您绝对路径。您需要向链接器指明在哪里可以找到这些库。

为此,您可以使用 target_link_directories() cmake 命令。 https://cmake.org/cmake/help/latest/command/target_link_directories.html

我猜你要传递的路径是“C:\opencv\build\x64\vc15\lib”。

【讨论】:

我在add_executable(project main.cpp)之后添加了这一行:target_link_directories(project PUBLIC C:\\opencv\\build\\x64\\vc15\\lib),但它给出了相同的错误信息

以上是关于即使在链接库时,也会获得对 OpenCV 元素的未定义引用。使用 CMake的主要内容,如果未能解决你的问题,请参考以下文章

即使使用 `-dec-math`,gfortran 也会对 `dacosd_` 给出未定义的引用

安装 pHash 库时对“fftw_init_threads”的未定义引用

OpenCV“对'cv :: imread'等C ++的未定义引用

使用 Android NDK 构建本机 OpenCV 会给出“对 'cv::String::deallocate()' 的未定义引用”

静态库的未定义引用和奇怪的内容

对 `_imp__SetupDiGetClassDevsA@16' 的未定义引用(即使使用 -lsetupapi)