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 aboveIs 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?