简体   繁体   中英

OpenCV undefined reference to `cv::imread(cv::String const&, int)'

I cannot get opencv working in C++ code. I've read all the similar question, but not have gotten my code to compile.

I installed OpenCV from source and included the -D OPENCV_GENERATE_PKGCONFIG=ON option and I'm trying to get a simple OpenCV example to work.

Here's the code:

    #include <opencv2/core.hpp>
    #include <opencv2/imgcodecs.hpp>
    #include <opencv2/highgui.hpp>
    #include <iostream>
    using namespace cv;
    int main()
    {
        std::string image_path = "/data/000001.jpg";
        Mat img = imread(image_path, IMREAD_COLOR);
        if(img.empty())
        {
            std::cout << "Could not read the image: " << image_path << std::endl;
            return 1;
        }
        imshow("Display window", img);
        int k = waitKey(0); // Wait for a keystroke in the window
        if(k == 's')
        {
            imwrite("starry_night.png", img);
        }
        return 0;
    }

Trying to compile with: $/usr/bin/g++ -g pkg-config --cflags --libs opencv main.cpp causes 'undefined reference' error for all OpenCV calls: /code/cpp/play/main.cpp:9: undefined reference to `cv::imread(cv::String const&, int)'

Running pkg-config --cflags --libs opencv returns:

pkg-config --cflags --libs opencv -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_stitching -vlopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_viz -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_imgproc -lopencv_core

I also tried to build the code with cmake using this file:

    set(CMAKE_VERBOSE_MAKEFILE ON)
    cmake_minimum_required(VERSION 3.16)
    project(play)
    set(CMAKE_CXX_STANDARD 14)
    find_package(OpenCV REQUIRED)
    message(STATUS "OpenCV_INCLUDE_DIRS = ${OpenCV_INCLUDE_DIRS}")
    message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
    add_executable(play main.cpp)

Is this what OpenCV_LIB is supposed to look like? Is there supposed to be a path?

-- OpenCV_INCLUDE_DIRS = /usr/include;/usr/include/opencv
-- OpenCV_LIBS = opencv_calib3d;opencv_core;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;opencv_viz;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
-- Configuring done

Thanks for the help.

Thanks for the suggestions. john was right. This solved it:

g++ -g main.cpp `pkg-config --libs opencv`

pkg-config needed to be after the source files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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