C++/CMake:Undefined reference to imwrite in OpenCV

2019-08-02 23:57发布

This is my CMakeLists.txt:

project(proj)
set(OpenCV_DIR "/usr/lib/opencv")
find_package(OpenCV REQUIRED COMPONENTS core imgproc highgui)

include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(test test.cpp>
target_link_libraries(test ${OpenCV_LIBS})

and this is part of my code:

#include <opencv/cv.h>
#include <opencv/cv.hpp>
#include <opencv/highgui.h>
#include <opencv/highgui.hpp>

using namespace cv;
using namespace std;

int main(int argv, char **argc)
{       
    //other code
    Mat img(height, width, CV_8UC3);
    //other code

    imwrite("/path", img);
    namedWindow( "Display window", 1 );
    imshow("Display window", img);

    waitKey(0);                                          
    return 0;

}

and the error message I get is:

undefined reference to cv::imwrite(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::Mat const&, std::vector<int, std::allocator<int> > const&)`

imshow works correctly, so I have no clue why imwrite is giving me an error.

EDIT:

g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs`

gives the same error.

1条回答
做自己的国王
2楼-- · 2019-08-03 00:31

In your find_package() command, you are only selecting some specific modules from the OpenCV package, and the one you need (imgcodecs which provides imwrite()) is not in the list.

Try changing it to:

find_package(OpenCV)
查看更多
登录 后发表回答