I installed two versions of openCV one is openCV3 and another one openCV2.4 on my ubuntu 16.04 machine and need both. However I am now unable to compile C++ code which is intended for openCV3. I get a ton of linker errors when trying to compile my code which always compiled perfectly fine before installing openvc2.4

The type of errors I get:

In file included from main.cpp:10:0:
/usr/local/include/opencv2/imgproc.hpp:4090:5: note: previous declaration ‘cv::ColormapTypes COLORMAP_PINK’
     COLORMAP_PINK = 10, //!< ![pink](pics/colormaps/colorscale_pink.jpg)
In file included from /usr/local/include/opencv2/opencv.hpp:59:0,
                 from include/faceDetection.h:6,
                 from main.cpp:11:
/usr/local/include/opencv2/contrib/contrib.hpp:980:24: error: redeclaration of ‘COLORMAP_HOT’
         COLORMAP_HOT = 11
In file included from main.cpp:10:0:
/usr/local/include/opencv2/imgproc.hpp:4091:5: note: previous declaration ‘cv::ColormapTypes COLORMAP_HOT’
     COLORMAP_HOT = 11, //!< ![hot](pics/colormaps/colorscale_hot.jpg)
main.cpp: In function ‘int main(int, char**)’:
main.cpp:89:65: error: call of overloaded ‘createFisherFaceRecognizer()’ is ambiguous
     Ptr<BasicFaceRecognizer> model = createFisherFaceRecognizer();
In file included from /usr/local/include/opencv2/face.hpp:373:0,
                 from main.cpp:8:
/usr/local/include/opencv2/face/facerec.hpp:99:39: note: candidate: cv::Ptr<cv::face::BasicFaceRecognizer> cv::face::createFisherFaceRecognizer(int, double)
 CV_EXPORTS_W Ptr<BasicFaceRecognizer> createFisherFaceRecognizer(int num_components = 0, double threshold = DBL_MAX);
In file included from /usr/local/include/opencv2/opencv.hpp:59:0,
                 from include/faceDetection.h:6,
                 from main.cpp:11:
/usr/local/include/opencv2/contrib/contrib.hpp:963:38: note: candidate: cv::Ptr<cv::FaceRecognizer> cv::createFisherFaceRecognizer(int, double)
     CV_EXPORTS_W Ptr<FaceRecognizer> createFisherFaceRecognizer(int num_components = 0, double threshold = DBL_MAX);
main.cpp:93:48: error: no matching function for call to ‘cv::face::BasicFaceRecognizer::train(std::vector<cv::Mat>&, std::vector<int>&)’
     model->train(trainImages, labelsTrainImages);
In file included from main.cpp:8:0:
/usr/local/include/opencv2/face.hpp:207:26: note: candidate: virtual void cv::face::FaceRecognizer::train(int, int)
     CV_WRAP virtual void train(InputArrayOfArrays src, InputArray labels) = 0;
/usr/local/include/opencv2/face.hpp:207:26: note:   no known conversion for argument 1 from ‘std::vector<cv::Mat>’ to ‘int’
main.cpp:123:64: error: no matching function for call to ‘cv::face::BasicFaceRecognizer::predict(cv::Mat&, int&, double&)’
                 model->predict(face, predictedLabel, confidence);
In file included from main.cpp:8:0:
/usr/local/include/opencv2/face.hpp:259:35: note: candidate: int cv::face::FaceRecognizer::predict(int) const
     CV_WRAP_AS(predict_label) int predict(InputArray src) const;

The command with which I compile:

g++ src/faceDetection.cpp src/faceRecognition.cpp main.cpp -L/usr/local/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm               -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo               -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot               -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect               -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core -o main 

This is what /usr/local/include looks like:

$ ls /usr/local/include/
        folly  opencv  opencv2  wangle

$ls /usr/local/include/opencv
        cvaux.h  cvaux.hpp  cv.h  cv.hpp  cvwimage.h  cxcore.h  cxcore.hpp  cxeigen.hpp  cxmisc.h  highgui.h  ml.h

$ ls /usr/local/include/opencv2
        aruco            contrib     dpm.hpp         fuzzy.hpp      imgproc.hpp          ocl                   plot.hpp      stereo.hpp            text          videostab
        aruco.hpp        core        face            gpu            legacy               opencv.hpp            reg           stitching             text.hpp      videostab.hpp
        bgsegm.hpp       core.hpp    face.hpp        hdf            line_descriptor      opencv_modules.hpp    rgbd          stitching.hpp         tracking      xfeatures2d
        bioinspired      cvconfig.h  features2d      hdf.hpp        line_descriptor.hpp  optflow               rgbd.hpp      structured_light      tracking.hpp  xfeatures2d.hpp
        bioinspired.hpp  cvv         features2d.hpp  highgui        ml                   optflow.hpp           saliency      structured_light.hpp  ts            ximgproc
        calib3d          cvv.hpp     flann           highgui.hpp    ml.hpp               phase_unwrapping      saliency.hpp  superres              video         ximgproc.hpp
        calib3d.hpp      datasets    flann.hpp       imgcodecs      nonfree              phase_unwrapping.hpp  shape         superres.hpp          video.hpp     xobjdetect.hpp
        ccalib           dnn         freetype.hpp    imgcodecs.hpp  objdetect            photo                 shape.hpp     surface_matching      videoio       xphoto
        ccalib.hpp       dnn.hpp     fuzzy           imgproc        objdetect.hpp        photo.hpp             stereo        surface_matching.hpp  videoio.hpp   xphoto.hpp

content of /usr/lib/x86_64-linux-gnu: http://paste.ubuntu.com/25261998/.


OpenCV3 installs it's headers into an opencv folder for the version 3 stuff and an opencv2 folder for the backwards compatibility stuff. When you did build and install OpenCV2.4 after OpenCV3, you did probably overwrite your headers. THe libraries itself are versioned, but not the headers.

I'm not sure whether it makes sense at all to have both versions installed, but if you have your reason, uninstall both to avoid a mess, reinstall OpenCV3 in the default location, then choose a different path prefix for OpenCV2, like /opt/opencv2.

Then, when building something against OpenCV2, provide /opt/opencv2/include as include path and set LD_LIBRARY_PATH=/opt/opencv2/lib (for building and execution).

If you use cmake to build your project, you'll have to adapt (or create) the FindOpencv.cmake script.

  • 1) what about my python code which uses opencv2? Doing import cv2 won't work any more as it isn't installed in its default location. 2) how do you know this: and an opencv2 folder for the backwards compatibility stuff? (I hadn't noticed it) – LandonZeKepitelOfGreytBritn Aug 19 '17 at 9:36
  • 1) Sorry, no idea. Never used opencv with python. Is there no migration guide for that? 2) I did the switch from opencv2 to opencv3 (without keeping an opencv2 installation). And I see the opencv2 folder inside opencv3. – Philippos Aug 19 '17 at 11:14
  • is it possible to do it the other way around to install openCV2 on its default location and openCV3 on a more "exotic" location, without too many difficulties? – LandonZeKepitelOfGreytBritn Sep 6 '17 at 7:15
  • In one project I use opencv3 in a custom location, while in the standard location a tegra-optimized opencv 2.4 is kept. The project is cmake-based, so I adapted the FIndOpencv.cmake to ignore the standard path and prefer the custom path and I have no problems whatsoever. But if you want to use other third party libs binding to opencv 3, you'll either have to work with LD_LIBRARY_PATH or /etc/ld.so.conf with ldconfig. I did not try that case. – Philippos Sep 6 '17 at 7:34

In Gentoo we support slotting... This means that we can have distinct versions from a package living together in the same system.

This is possible because our package manager (portage) creates symblinks with the general name from the package and different names for the versions.

For example, if you have python 2 and 3...there is only one symblink called python that points to a specific version (2 or 3) and you have a script that handles which of the versions is the symblink pointing.

I suppose you could do the same with the headers, creating a opencv.h and have two different header files... Then you could link the original to one of your .h files.

You could give gentoo a try if you don't want to do it by yourself and create the same logic that we already have.


In your shared library folder both version of the so-files are installed. When you specify -l gcc defaults to the higher SONAME - which seems to be 2.4 in your case. Maybe this answer on Stack Overflow helps you (2nd answer is the best IMHO).

  • 1
    But the problem doesn't arise on linking, but on compiling due to header inconsistency, it seems. – Philippos Aug 17 '17 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.