0
votes

I have installed opencv2.

pkg-config --modversion opencv

2.4.13.5

I am using below code,

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

using namespace std;
using namespace cv;

int main(int argc, const char *argv[])
{
    VideoCapture vid(0);
    if(!vid.isOpened()){
        cout<<"Camera could not load..."<<endl;
        return -1;
    }
    namedWindow("webcam",CV_WINDOW_AUTOSIZE);
    while(1){
        Mat frame;
        bool ctrl = vid.read(frame);
        imshow("webcam",frame);
        if(waitKey(0) == 27){
            cout<<"The app is ended..."<<endl;
            break;
        }
    }
    return 0;
}

I compiled using, g++ image_writer.cpp

/tmp/ccPWmgA0.o: In function main': image_writer.cpp:(.text+0x38): undefined reference tocv::VideoCapture::VideoCapture(int)' image_writer.cpp:(.text+0x47): undefined reference to cv::VideoCapture::isOpened() const' image_writer.cpp:(.text+0xac): undefined reference tocv::namedWindow(std::__cxx11::basic_string, std::allocator > const&, int)' image_writer.cpp:(.text+0xe9): undefined reference to cv::VideoCapture::read(cv::Mat&)' image_writer.cpp:(.text+0x105): undefined reference tocv::_InputArray::_InputArray(cv::Mat const&)' image_writer.cpp:(.text+0x148): undefined reference to cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)' image_writer.cpp:(.text+0x170): undefined reference tocv::waitKey(int)' image_writer.cpp:(.text+0x1cd): undefined reference to cv::VideoCapture::~VideoCapture()' image_writer.cpp:(.text+0x254): undefined reference tocv::VideoCapture::~VideoCapture()' /tmp/ccPWmgA0.o: In function cv::Mat::~Mat()': image_writer.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference tocv::fastFree(void*)' /tmp/ccPWmgA0.o: In function cv::Mat::release()': image_writer.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference tocv::Mat::deallocate()' collect2: error: ld returned 1 exit status

After surfing I found, I have to include lib file

so, I did this. g++ image_writer.cpp -lopencv_videoio

/usr/bin/ld: cannot find -lopencv_videoio collect2: error: ld returned 1 exit status

I don't know how to fix this. please help me how to fix this. Any hint would be appreciable.

EDIT-1:

pkg-config --libs opencv returns,

-L/usr/local/lib -lopencv_contrib -lopencv_stitching -lopencv_nonfree -lopencv_superres -lopencv_ocl -lopencv_ts -lopencv_videostab -lopencv_gpu -lopencv_photo -lopencv_objdetect -lopencv_legacy -lopencv_video -lopencv_ml -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_imgproc -lopencv_flann -lopencv_core -lQtCore -lQtTest -lQtGui -lQtOpenGL -lIlmThread -lHalf -lIex -lIlmImf -lImath -ljasper -ltiff -lpng -ljpeg -lswscale-ffmpeg -lavutil-ffmpeg -lavformat-ffmpeg -lavcodec-ffmpeg -lv4l2 -lv4l1 -ldc1394 -lgstpbutils-0.10 -lgstriff-0.10 -lgstapp-0.10 -lgstvideo-0.10 -lxml2 -lglib-2.0 -lgthread-2.0 -lgmodule-2.0 -lgobject-2.0 -lgstreamer-0.10 -lgstbase-0.10 -lGLU -lGL -lz -latomic -ltbb -lrt -lpthread -lm -ldl -lstdc++

2
You can list the linker flags and libraries needed using pkg-config --libs opencv.Some programmer dude
@Someprogrammerdude- please look at the updated postMohamed Thasin ah

2 Answers

4
votes

The OpenCV "library" is really a collection of multiple libraries, and you usually need to link with multiple libraries.

The simple solution is to use pkg-config --libs opencv to get all linker flags and libraries that are needed:

g++ image_writer.cpp `pkg-config --libs opencv`
3
votes

compilation using cmake

cmake_minimum_required(VERSION 2.8)

project(ImageWriter)
add_executable(${PROJECT_NAME} "image_writer.cpp")    
FIND_PACKAGE(OpenCV REQUIRED )
include_directories(${OPENCV_INCLUDE_DIRS})
message(" cv_libs:     " ${OpenCV_LIBS})
message(" cv_includes: " ${OPENCV_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS})