3

I have seen posts on this before, but none of the answers seemed to work for me. I just installed OpenCV on my Mac and got it to work fine with g++ from the command line. I installed it using Mac Ports:

$ sudo port install opencv configure.compiler=llvm-gcc-4.2

To compile with g++, I use the command:

g++ myfile.cpp -o myprogram -I/opt/local/include -L/opt/local/lib -lopencv_core.2.4.2 -lopencv_calib3d.2.4.2

And my OpenCV include header in the main.cpp file is:

#include "opencv2/opencv.hpp"

And all works well.

Now, when I tried to include the library in Xcode, it simply says 'opencv2/opencv.hpp' file not found. To include the library in Xcode, I followed some detailed instructions. First, to the project target, I added /opt/local/lib to the library search paths, and /opt/local/include, /opt/local/include/opencv and /opt/local/include/opencv2 to the header search paths under the Build Settings tab. Then I clicked on Build Phases, and to Link Binary With Libraries I added all of the OpenCV .dylib files in /opt/local/lib (all 11 of them). This didn't work. I've tried many other things, including adding to user search paths and setting up "Other Linker Flags" for the target. Nothing worked. It can't find the file. If I use the exact path (#include "/opt/local/include/opencv2/opencv.hpp") it can't find all the other header files.

The version of OpenCV is 2.4.2. I am using Xcode 4.3.3 on OS X Lion 10.7.4. This is a C++ project. Any ideas?

2
  • Any luck since you posted the question? Take a look at my answer here and see if it helps. Note that my installation is in /usr/local, not /opt/local so be sure to make the correct substitution.
    – SSteve
    Jul 30, 2012 at 21:47
  • I find it best to use CMake to generate my XCode project files. That way I dont have to bother with finding all the right libraries. FindLibrary.cmake files are available for most libraries you would use in computer vision applications.
    – ssb
    Jan 17, 2013 at 11:47

1 Answer 1

3

This problem can be solved by going to the project properties and click on the target. In the build settings search "header search path". And there you add "/opt/local/include". By doing this you're telling Xcode to look for the header files in that directory also. Now you can use:

#include <opencv2/opencv.hpp>

If you don't want to do this every time you start a new project you can add the .h and .hpp files to the defaulti include directory Xcode uses. You can find this directory in Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include .

You will have to add the binaries to your project as well. For that you go to "build phases" and then add the binaries in the "Link binaries with libraries" section.

I hope this helps you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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