opencv неопределенная ссылка на imread с CMake на Windows10 VS Code - PullRequest
0 голосов
/ 11 сентября 2018

Я хочу запустить OpenCV в VS Code IDE с CMake, но он показывает undefined reference to cv::imread().Я пробовал несколько способов (например, редактировать переменные PATH ...), чтобы решить проблему, однако он все еще может ссылаться на библиотеку opencv.

  • Вот мой CMakelists.txt

    # cmake needs this line
    cmake_minimum_required(VERSION 2.8)
    
    # Enable C++11
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
    
    # Define project name
    project(cvtest)
    
    # Find OpenCV, you may need to set OpenCV_DIR variable
    # to the absolute path to the directory containing OpenCVConfig.cmake file
    # via the command line or GUI
    find_package(OpenCV REQUIRED)
    
    set(OpenCV_INCLUDE_DIRS "C:/opencv/build/include")
    include_directories(${OpenCV_INCLUDE_DIRS})
    
    # If the package has been found, several variables will
    # be set, you can find the full list with descriptions
    # in the OpenCVConfig.cmake file.
    # Print some message showing some of them
    message(STATUS "OpenCV library status:")
    message(STATUS "    version: ${OpenCV_VERSION}")
    message(STATUS "    libraries: ${OpenCV_LIBS}")
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
    
    # Declare the executable target built from your sources
    add_executable(cvtest cvtest.cpp)
    
    # Link your application with OpenCV libraries
    target_link_libraries(cvtest ${OpenCV_LIBS})
    
  • После завершения настроек CMake я запускаю $ cmake -G "MinGW Makefiles".

    -- OpenCV library status:
    --     version: 2.4.13.6
    --     libraries: opencv_videostab;opencv_video;opencv_ts;opencv_superres;opencv_stitching;opencv_photo;opencv_ocl;opencv_objdetect;opencv_nonfree;opencv_ml;opencv_legacy;opencv_imgproc;opencv_highgui;opencv_gpu;opencv_flann;opencv_features2d;opencv_core;opencv_contrib;opencv_calib3d
    --     include path: C:/opencv/build/include
    
  • Наконец, я запускаю $ mingw32-make, но вышло с некоторыми ошибками

    Scanning dependencies of target cvtest
    [ 50%] Building CXX object CMakeFiles/cvtest.dir/cvtest.cpp.obj
    [100%] Linking CXX executable cvtest.exe
    CMakeFiles\cvtest.dir/objects.a(cvtest.cpp.obj):cvtest.cpp:(.text+0x63): 
    undefined reference to cv::imread(std::string const&, int)'
    CMakeFiles\cvtest.dir/objects.a(cvtest.cpp.obj):cvtest.cpp:(.text+0x87): 
    undefined reference to cv::_InputArray::_InputArray(cv::Mat const&)'
    CMakeFiles\cvtest.dir/objects.a(cvtest.cpp.obj):cvtest.cpp:(.text+0xc1): 
    undefined reference to cv::imshow(std::string const&, cv::_InputArray const&)'
    c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: 
    CMakeFiles\cvtest.dir/objects.a(cvtest.cpp.obj): bad reloc address 0xf in section `.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]'
    c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: 
    final link failed: Invalid operation
    collect2.exe: error: ld returned 1 exit status
    CMakeFiles\cvtest.dir\build.make:104: recipe for target 'cvtest.exe' failed
    mingw32-make[2]: *** [cvtest.exe] Error 1
    CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/cvtest.dir/all' failed
    mingw32-make[1]: *** [CMakeFiles/cvtest.dir/all] Error 2
    Makefile:82: recipe for target 'all' failed
    mingw32-make: *** [all] Error 2
    
  • Вот cvtest.cpp

    #include <cstdio>
    #include <iostream>
    #include <opencv2/opencv.hpp>
    
    using namespace cv;
    using namespace std;
    
    int main(int argc, char *argv[]) {
        Mat img = imread("lena.bmp", CV_LOAD_IMAGE_COLOR);
        imshow("lena", img);
    }
    

СделалЯ неправильно понимаю что-то, что я должен знать, и как я могу это исправить?Спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...