0

I am on OSX 10.10.5 and new to opencv.

Before realizing I had an older version of opencv installed (2.4.6), I installed the latest opencv3 with the following:

brew tap homebrew/science
brew install opencv3 --with-contrib 

I am failing at linking a simple opencv example from the command line with either the old opencv library or the new one.

Here is my Makefile:

CC = g++

# Use the following to access new install of opencv3
#CFLAGS = -I/usr/local/Cellar/opencv3/3.1.0_3/include
#LDFLAGS =  -L /usr/local/Cellar/opencv3/3.1.0_3/lib/ -lm -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc

# Use the following to access old install of opencv (2.4.6) 
CFLAGS = -I/opt/local/include
LDFLAGS =  -L /opt/local/lib/ -lm -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc
ALL = vision

all: $(ALL)

vision: vision.o
    $(CC) $(LDFLAGS) -o $@ $^

vision.o: vision.cpp
    $(CC) $(CFLAGS) -c $<

.PHONY: clean

clean:
    rm -rf *.o core* $(ALL)

In both cases, the file compiles but fails to link with the same errors:

Undefined symbols for architecture x86_64:
  "cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in vision.o
  "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in vision.o
...

And yet the opencv libs are all there:

Old 2.4.6 ones (highgui for example though the other ones are present as well):

$ ls -l /opt/local/lib/libopencv_highgui*
-rwxr-xr-x  1 root  wheel  328972 Sep 23  2013 /opt/local/lib/libopencv_highgui.2.4.6.dylib
lrwxr-xr-x  1 root  wheel      29 Oct  8  2013 /opt/local/lib/libopencv_highgui.2.4.dylib -> libopencv_highgui.2.4.6.dylib
lrwxr-xr-x  1 root  wheel      27 Oct  8  2013 /opt/local/lib/libopencv_highgui.dylib -> libopencv_highgui.2.4.dylib

Or new ones:

$ ls -l /usr/local/Cellar/opencv3/3.1.0_3/lib/libopencv_highgui.*
-r--r--r--  1 lolo  admin  61752 Jul 22 15:27 /usr/local/Cellar/opencv3/3.1.0_3/lib/libopencv_highgui.3.1.0.dylib
lrwxr-xr-x  1 lolo  admin     29 Jul 22 15:27 /usr/local/Cellar/opencv3/3.1.0_3/lib/libopencv_highgui.3.1.dylib -> libopencv_highgui.3.1.0.dylib
lrwxr-xr-x  1 lolo  admin     27 Jul 22 15:27 /usr/local/Cellar/opencv3/3.1.0_3/lib/libopencv_highgui.dylib -> libopencv_highgui.3.1.dylib

For reference, the sample code I am trying to run:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/imgcodecs.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( )
{

       Mat image;

       // LOAD image
       image = imread("image1.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file "image.jpg".
              //This file "image.jpg" should be in the project folder.
              //Else provide full address : "D:/images/image.jpg"

       if(! image.data )  // Check for invalid input
       {
              cout <<  "Could not open or find the image" << std::endl ;
              return -1;
       }

       //DISPLAY image
       namedWindow( "window", CV_WINDOW_AUTOSIZE ); // Create a window for display.
       imshow( "window", image ); // Show our image inside it.

       //SAVE image
       imwrite("result.jpg",image);// it will store the image in name "result.jpg"

       waitKey(0);                       // Wait for a keystroke in the window
       return 0;
}

Also my current package config though I don't think it should matter given how I am linking:

pkg-config --cflags --libs opencv
-I/opt/local/include/opencv -I/opt/local/include  /opt/local/lib/libopencv_calib3d.dylib /opt/local/lib/libopencv_contrib.dylib /opt/local/lib/libopencv_core.dylib /opt/local/lib/libopencv_features2d.dylib /opt/local/lib/libopencv_flann.dylib /opt/local/lib/libopencv_gpu.dylib /opt/local/lib/libopencv_highgui.dylib /opt/local/lib/libopencv_imgproc.dylib /opt/local/lib/libopencv_legacy.dylib /opt/local/lib/libopencv_ml.dylib /opt/local/lib/libopencv_nonfree.dylib /opt/local/lib/libopencv_objdetect.dylib /opt/local/lib/libopencv_photo.dylib /opt/local/lib/libopencv_stitching.dylib /opt/local/lib/libopencv_superres.dylib /opt/local/lib/libopencv_ts.dylib /opt/local/lib/libopencv_video.dylib /opt/local/lib/libopencv_videostab.dylib  

------- UPDATE TO GET IT WORKING AND FOLLOW-UP QUESTION -------

Thanks to Mark, I was able to compile the code though I am not too sure I did this right.

First, I changed the linking command to rely on pkg-config and use opencv3:

g++ $(pkg-config --cflags --libs /usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc) vision.cpp -o test

This got me an error:

ld: library not found for -lippicv

I then added a symbolic link to that library from the lib folder used by opencv3 and that did the trick.

$ ln -s /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a /usr/local/Cellar/opencv3/3.1.0_3/lib/

That brings up a few questions for my own understanding:

  • Is that the right approach?

  • I would expect g++ $(pkg-config --cflags --libs opencv) vision.cpp -o test to work as well by using the older opencv library I have installed but it produces the same "symbol not found" errors reported above

  • Is there a way to permanently change the pkg config so that pkg-config --cflags --libs opencv picks up the opencv3 package config without my having to specify the full path in the future?

Lolo
  • 3,935
  • 5
  • 40
  • 50
  • 1
    First you need to decide if you want to use OpenCV 2 or OpenCV 3. Then you need to use the `pkg-config` that corresponds to the version you want in your Makefile and everything will work. It's no use installing OpenCV 3 and then using the `pkg-config` that is for OpenCV 2. – Mark Setchell Jul 28 '16 at 21:25
  • 1
    http://stackoverflow.com/a/36999364/2836621 – Mark Setchell Jul 28 '16 at 21:27
  • http://stackoverflow.com/a/34341219/2836621 – Mark Setchell Jul 28 '16 at 21:28
  • I'd rather start with opencv3. I followed your answer from the other post and that got me a step closer: g++ $(pkg-config --cflags --libs /usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc) vision.cpp -o test now "only" complains about ld: library not found for -lippicv. I think I recall reading a post about that error somewhere... – Lolo Jul 28 '16 at 21:32
  • 1
    Sorry, I am currently nowhere near a Mac to help out properly so I am just trying to nudge you in the right direction. You can always take the output from `pkg-config` as a baseline and temporarily alter it till you can resolve any difficulties later... e.g. by removing `-lippicv` temporarily. – Mark Setchell Jul 28 '16 at 21:36
  • Figured it out: ln -s /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a /usr/local/Cellar/opencv3/3.1.0_3/lib/ My code compiles now. You nudged me in the right direction! Thanks. I will update my question accordingly. – Lolo Jul 28 '16 at 21:37
  • Cool - good luck with your project and remember questions & answers are free so come back if you get stuck! – Mark Setchell Jul 28 '16 at 21:40
  • By the way, if you run `brew options opencv3` you will see the other options you can add. I find the one regarding `--with-qt` or somesuch is very useful as it gives you extra options to enlarge and save images when you call `imshow()`. – Mark Setchell Jul 28 '16 at 21:57

0 Answers0