Compiling OpenCV 2 on Ubuntu

by saliksyed

Getting OpenCV to compile is often quite a pain in the butt. I gave up trying to get it to work on Mac OSX and decided to use Ubuntu 11.04 instead. Turns out it was non-trivial to get it working on here too!

Here are the errors I encountered and how I fixed them (much credit to google and scouring a few forums!)

Error #1:

> /usr/include/libavutil/common.h: In function 'int32_t av_clipl_int32(int64_t)':
> /usr/include/libavutil/common.h:154:47: error: 'UINT64_C' was not declared in this scope

Fix: You need to modify the file CMakeLists.txt and add in the following lines:

set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -D__STDC_CONSTANT_MACROS")

This should be under the if statement which looks like this:

if(CMAKE_COMPILER_IS_GNUCXX)
# High level of warnings.

Error #2:

../../lib/libopencv_highgui.so.2.2.0: undefined reference to `cvCreateCameraCapture_V4L(int)'
collect2: ld returned 1 exit status
make[2]: *** [bin/opencv_createsamples] Error 1
make[1]: *** [modules/haartraining/CMakeFiles/opencv_createsamples.dir/all] Error 2
make: *** [all] Error 2

Fix:
If you need webcam support then this unfortunately will disable it. But this is the best solution I’ve found so far. Find the following portion of code in the CMakeLists.txt

 if(WITH_V4L)
    CHECK_MODULE(libv4l1 HAVE_LIBV4L)
    CHECK_INCLUDE_FILE(linux/videodev.h HAVE_CAMV4L)
    CHECK_INCLUDE_FILE(linux/videodev2.h HAVE_CAMV4L2)
else()
    set(HAVE_LIBV4L FALSE)
    set(HAVE_CAMV4L FALSE)
    set(HAVE_CAMV4L2 FALSE)
endif()

and replace with just:

set(HAVE_LIBV4L FALSE)
set(HAVE_CAMV4L FALSE)
set(HAVE_CAMV4L2 FALSE)
Advertisements