14

I've just followed the instructions to install opencv 3 on my ubuntu computer (I've already installed and ran programs using opencv 3 on a different linux device), however, when I tried to run one of my test programs from my old computer I get the following errors:

CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `main':
VideoTest.cpp:(.text+0x6f): undefined reference to `cv::VideoWriter::fourcc(char,char,char, char)'
VideoTest.cpp:(.text+0xc3): undefined reference to `cv::VideoWriter::open(cv::String const&, int, double, cv::Size_<int>, bool)'
VideoTest.cpp:(.text+0x103): undefined reference to `cv::namedWindow(cv::String const&, int)'
VideoTest.cpp:(.text+0x146): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)'
VideoTest.cpp:(.text+0x1b1): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `cv::String::String(char const*)':
VideoTest.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x3b): undefined reference to `cv::String::allocate(unsigned int)'
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `cv::String::~String()':
VideoTest.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0xd): undefined reference to `cv::String::deallocate()'
collect2: ld returned 1 exit status
make[2]: *** [VideoTest] Error 1
make[1]: *** [CMakeFiles/VideoTest.dir/all] Error 2
make: *** [all] Error 2

Any idea what's going on? I'm relatively new to opencv. Here's my test code for reference:

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

using namespace cv;
using namespace std;

int main(int argc, char* argv[]){
    VideoCapture vCap(0);
    VideoWriter vWrite;
    vWrite.open("test.avi", vWrite.fourcc('M','J','P','G'), 20, Size(640, 480), true);
    while (1) {
        namedWindow("VideoFeed", WINDOW_AUTOSIZE);
        Mat frame;
        vCap.read(frame);
        vWrite.write(frame);
        imshow("VideoFeed", frame);
        char c = waitKey(50);
        if (c == 27) {
            break;
        }
    }
    return 0;
}

Thanks in advance.

Edit: My CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(VideoTest)
find_package(OpenCV REQUIRED)
add_executable(VideoTest VideoTest.cpp)
target_link_libraries(VideoTest ${OpenCV_LIBS})
potatoprogrammer
  • 201
  • 1
  • 2
  • 7
  • 1
    Similar Issue [Solved] [http://stackoverflow.com/questions/7816607/opencv-2-3-compiling-issue-undefined-refence-ubuntu-11-10](http://stackoverflow.com/questions/7816607/opencv-2-3-compiling-issue-undefined-refence-ubuntu-11-10) – IEEE754 Oct 12 '15 at 06:26

3 Answers3

27

VideoCapture and VideoWriter were moved to the videoio module in 3.0, so you have to additionally

#include "opencv2/videoio.hpp"

also, you don't seem to link to any of the required opencv libs, those are:

opencv_core, opencv_videoio, opencv_highgui
berak
  • 39,159
  • 9
  • 91
  • 89
  • I'm not sure what you mean by linking to the required opencv libs. Could you tell me how to do that specifically? – potatoprogrammer Sep 16 '14 at 20:32
  • you did not tell us your os, the compiler used, also you seem to use cmake to generate things, so please add the cmakelists.txt to your question, then let's re-iterate – berak Sep 16 '14 at 20:35
  • My os version is Ubuntu 12.04.5 LTS, and I'm using cmake 2.8.7 and gcc 4.6.3. Added my CMakeLists.txt into my question. – potatoprogrammer Sep 16 '14 at 20:43
  • sorry, did not see the tags. unfortunately no real idea, what failed now, you cmake file looks quite ok to me. – berak Sep 16 '14 at 20:46
  • huh, weird. Thanks for trying to help me, anyway – potatoprogrammer Sep 16 '14 at 20:52
  • 1
    you can still fall back to (manual) `g++ VideoTest.cpp -lopencv_core -lopencv_videoio -lopencv_highgui -o VideoTest` – berak Sep 16 '14 at 20:56
5

Alright, I've finally figured out what's going on. Something internally in the CMakeFiles folder went screwy when I moved the directory over to my new system. Removing all files/directories besides the project .cpp file and the CMakeLists.txt file, and running "cmake ." and "make" again seems to solve the problem.

potatoprogrammer
  • 201
  • 1
  • 2
  • 7
4

Below cmake file works for me.

cmake_minimum_required(VERSION 3.5)
project(ImageProcessing)

set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(SOURCE_FILES main.cpp)
add_executable(ImageProcessing ${SOURCE_FILES})
target_link_libraries( ImageProcessing ${OpenCV_LIBS} )
# 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}")