【问题标题】:OpenCV CMake undefined reference to functionOpenCV CMake对函数的未定义引用
【发布时间】:2020-06-26 12:28:39
【问题描述】:

我在使用 CMake 构建简单的 OpenCV 应用程序时遇到问题。 我花了很多时间才知道出了什么问题,但我仍然完全不知道......

我在 Ubuntu 18.04 上使用 CLion 作为 IDE

我的CMakeList.txt

cmake_minimum_required(VERSION 3.15)
project(qt_segmentation)

set(CMAKE_CXX_STANDARD 11)

find_package( OpenCV REQUIRED )
add_executable(qt_segmentation main.cpp)

target_include_directories(qt_segmentation PUBLIC ${OpenCV_INCLUDE_DIRS})
target_link_libraries(qt_segmentation ${OPENCV_LIBS})

ma​​in.cpp

#include <iostream>
#include <opencv2/opencv.hpp>

int main()
{
    std::cout<<cv::getVersionRevision()<<std::endl;
    return 0;
}

编译器本身似乎工作正常我可以使用以下命令成功编译我的文件:

g++ -std=c++11 main.cpp `pkg-config --libs --cflags opencv` -o main

错误:

====================[ Build | qt_segmentation | Debug ]=========================
/home/bienqq/clion-2019.3.4/bin/cmake/linux/bin/cmake --build /home/bienqq/CLionProjects/qt-segmentation/cmake-build-debug --target qt_segmentation -- -j 2
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bienqq/CLionProjects/qt-segmentation/cmake-build-debug
[ 50%] Linking CXX executable qt_segmentation
CMakeFiles/qt_segmentation.dir/main.cpp.o: In function `main':
/home/bienqq/CLionProjects/qt-segmentation/main.cpp:6: undefined reference to `cv::getVersionRevision()'
CMakeFiles/qt_segmentation.dir/main.cpp.o: In function `cv::String::~String()':
/usr/local/include/opencv2/core/cvstd.hpp:648: undefined reference to `cv::String::deallocate()'
CMakeFiles/qt_segmentation.dir/main.cpp.o: In function `cv::String::operator=(cv::String const&)':
/usr/local/include/opencv2/core/cvstd.hpp:656: undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
CMakeFiles/qt_segmentation.dir/build.make:83: recipe for target 'qt_segmentation' failed
make[3]: *** [qt_segmentation] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/qt_segmentation.dir/all' failed
make[2]: *** [CMakeFiles/qt_segmentation.dir/all] Error 2
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/qt_segmentation.dir/rule' failed
make[1]: *** [CMakeFiles/qt_segmentation.dir/rule] Error 2
Makefile:118: recipe for target 'qt_segmentation' failed
make: *** [qt_segmentation] Error 2

我还打印了 CMake 属性的值:

OpenCV_INCLUDE_DIRS=/usr/local/include;/usr/local/include/opencv

OpenCV_LIBS=opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab

从我的角度来看,我已经检查了所有内容,但仍然无法正常工作......

你能帮忙吗? 谢谢:)

【问题讨论】:

    标签: c++ opencv ubuntu cmake clion


    【解决方案1】:

    经过数小时的调查,CMake 似乎区分大小写: 更改时:

    target_link_libraries(qt_segmentation ${OPENCV_LIBS})
    

    target_link_libraries(qt_segmentation ${OpenCV_LIBS})
    

    作为 CMake 中的属性建议:

    OpenCV_LIBS=opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab
    
    

    Everythink 运行良好

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2016-06-26
      • 2015-05-12
      • 2018-01-01
      • 2012-06-23
      相关资源
      最近更新 更多