HOWTO: Setup XCode 6.1 to work with OpenCV3 libraries

Overview

This post demonstrates how to setup your XCode IDE to work with openCV libraries on Yosemite. Initially XCode can be an intimidating environment, but past all the windows and complex sections, it boasts a number of beneficial features that aid and simplify programming – which for newcomers to openCV are a must. Features such as autocomplete and integrated compiler are extremely useful in simplifying compilation, speeding up programming time and clarifying how pieces of code fit together.

WARNING: This guide assumes you have compiled and installed openCV
It not, see this guide.

How To

Let’s begin.

  • Open XCode
  • Create New Project
  • Under OSX Application > Choose Command Line Tool
  • Give your application a name then Save

Assigning the Search Paths

To use the openCV libraries you will need to tell XCode where to find them.

  • Click the XCode project file in your inspector (which is the blue icon in the furthest left hand tab). You should now have three tabs in the centre window. Build Settings | Build Options | Build Rules
  • Click Build Settings
  • Scroll down until you find Search Paths
    Screen Shot 2014-11-19 at 09.30.10 (2)
  • Double Click the Header Search Paths option, then Click the plus icon
  • Type in the following /usr/local/include
    Screen Shot 2014-11-19 at 09.32.25 (2)
  • Next Double Click the Library Search Paths option, then Click the plus icon
  • Type in the following /usr/local/lib

Linking the Libraries

Now we need to tell XCode which libraries we want to build against. Still in Build Settings.

  • Scroll until you find Linking
  • Double Click Other Linker Flags and Click the plus icon
  • Copy and Paste the following, then press enter
  • -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab

    Screen Shot 2014-11-19 at 09.37.29 (2)

Setting the Build Path

XCode automatically stores the executable file in a derived data folder, we want to change it so that the executable is stored in your project directory.

  • Open XCode Preferences
  • Select Locations Tab
    Screen Shot 2014-11-19 at 09.52.11 (2)
  • Click Advanced
  • Change the Location Button from Unique to Legacy
    Screen Shot 2014-11-19 at 09.52.20 (2)

Build your Application

Now you are ready to build your application. In your main.cpp file, add the following code.
This is a sample application from the openCV package.

#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

//hide the local functions in an anon namespace
namespace {
    void help(char** av) {
        cout << "The program captures frames from a video file, image sequence (01.jpg, 02.jpg ... 10.jpg) or camera connected to your computer." << endl
        << "Usage:\n" << av[0] << " <video file, image sequence or device number>" << endl
        << "q,Q,esc -- quit" << endl
        << "space   -- save frame" << endl << endl
        << "\tTo capture from a camera pass the device number. To find the device number, try ls /dev/video*" << endl
        << "\texample: " << av[0] << " 0" << endl
        << "\tYou may also pass a video file instead of a device number" << endl
        << "\texample: " << av[0] << " video.avi" << endl
        << "\tYou can also pass the path to an image sequence and OpenCV will treat the sequence just like a video." << endl
        << "\texample: " << av[0] << " right%%02d.jpg" << endl;
    }
    
    int process(VideoCapture& capture) {
        int n = 0;
        char filename[200];
        string window_name = "video | q or esc to quit";
        cout << "press space to save a picture. q or esc to quit" << endl;
        namedWindow(window_name, WINDOW_KEEPRATIO); //resizable window;
        Mat frame;
        
        for (;;) {
            capture >> frame;
            if (frame.empty())
                break;
            
            imshow(window_name, frame);
            char key = (char)waitKey(30); //delay N millis, usually long enough to display and capture input
            
            switch (key) {
                case 'q':
                case 'Q':
                case 27: //escape key
                    return 0;
                case ' ': //Save an image
                    sprintf(filename,"filename%.3d.jpg",n++);
                    imwrite(filename,frame);
                    cout << "Saved " << filename << endl;
                    break;
                default:
                    break;
            }
        }
        return 0;
    }
}

int main(int ac, char** av) {
    
    if (ac != 2) {
        help(av);
        return 1;
    }
    std::string arg = av[1];
    VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file or image sequence
    if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
        capture.open(atoi(arg.c_str()));
    if (!capture.isOpened()) {
        cerr << "Failed to open the video device, video file or image sequence!\n" << endl;
        help(av);
        return 1;
    }
    return process(capture);
}

Then hold CMD + B (This builds and compiles the application)
Go to your project folder. There should be a new folder called Debug, inside is your executable file.
Open Terminal then navigate to the folder, then simply type ./<yourprojectname> 0
This should open window and display the camera image.
And there you go!

15 thoughts on “HOWTO: Setup XCode 6.1 to work with OpenCV3 libraries

  1. Hi,
    I’m getting a build failed error:
    “main.cpp:9:10: ‘opencv2/imgcodecs.hpp’ file not found”

    Seems related to this line of code
    “#include ”
    Do you know what’s the issue ? Or how I should redefined the path
    -thanks

    • Hi Chris,

      Could be one of two things.

      How did you install opencv?
      If the libs aren’t in the usr/include/ or usr/local folders then the compiler wont find them.
      Also when you link the libraries, you need to point the compiler at the -lopencv_imgcodecs?
      Is there any more error data you could post?

      D

    • Hi Chris,

      Can’t seem to reproduce this error.
      How did you make/install the libraries? Might be that it hasn’t built those files.
      See this guide.

      David

  2. Hi, David
    I have the libraries in /usr/local/lib and following your steps I did add -lopencv_imgcodecs
    I do have opencv and opencv2 in /usr/local/include. (no lib if that makes a difference)

    Im new to mac – Which more data could I post ?

  3. So looking at the libraries I see that I have opencv2/imgcodecs.hpp and opencv2/video/video.hpp
    I do have opencv2/highgui/highgui.hpp. Where can I get the missing files?

    Thanks

  4. I also discovered the missing opencv lib files. 2.4.10 and 2.4.9 are missing those modules in the zip files. I had to got download opencv_master on github and rerun CMake to be able to install those libraries.

    Which then opened another issue. I’m running OSX 10.10, Xcode 6.1, and CPT 6.1. When I compile an example in Xcode, I get 201 architecture symbolic link errors. It’s acting like it doesn’t want to cooperate with the static libopencv_*.a files. I’ve tried quite a few combinations over the past two days but to no avail. Any thoughts?

  5. You will also need to remove a few of the linker flags that are not associated with the library

    I removed the following and things worked:
    -lopencv_imgcodecs
    -lopencv_shape
    -lopencv_videoio

    • Cheers Oleg, I have updated the guides title. Once those flags are removed the principle should still be the same.

  6. Hi, when I run the program I still have one error:

    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    I followed the advice of Oleg and it gives me 183 more errors… so I don’t know what to do, do you have any idea?

    Thank you for your help!

    Tom

  7. Hi again,

    I’m using OpenCV 2.4.10 and Xcode 6.3, In my SharedLibs I can find a doc named opencv2 and I tried to do the way you did and I have tested with your example, then, I tried with the example of Oleg but it gives me one errors… When I have removed the files he adviced, I got 183 more errors…

    Best

  8. Hi,
    It’s sayd:
    ld: library not found for -lopencv_videoio
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    I probably need to download -lopencv_videoio, do you have this file? You could maybe send me it by mail or something else? Because when i watched in my file “lib” I obviously didn’t find it..This is probably the problem.

    Waiting for your answer,
    Best Tom

    • Hi Tom,

      Sorry just tested it against the 2.4.10 libs.

      If you’ve installed that lib successfully. In Xcode change the -lopencv_videoio linker flag to be -lopencv_video. Its a different name in openCV3.0.0 land.

      Best

  9. Hi again David!

    The thing is, I already have the linker flag -lopencv_video, AND, what is really funny, is that I also have -lopencv_videoio. But, when I delete the “videoio” in the linker flags, it gives me 183 errors in my programm, when I put it back, I only have one error. I have described the error in my past comment but when I watch in my libraries, I don’t find the -lopencv_videoio… It’s very strange.. can you use something to watch in my computer or something, I tried everything but no way…
    Thank you for your time!

    Tom

Comments are closed.