1

I'm just starting to work in Xcode and trying to get my (C++) command-line application (Mac OS X target) to include its own dynamic libraries for OpenCV so that I can deploy to other people's computers and it'll find them properly (without their computers needing to have OpenCV installed). But it's not looking anywhere except where they live on my computer (/opt/local/lib).

When the executable runs on another system, it throws errors like this:

dyld: Library not loaded: /opt/local/lib/libopencv_core.2.3.dylib Referenced from: /Users/editc/Desktop/edge_display/edge_display Reason: image not found

(Note that this is despite the fact that libopencv_core.2.3.dylib is included in the same directory as the executable.)

How do I get it to look in the application's own directory? Adding "./" to the library search paths seems to do nothing.

Several searches have indicated that it should look in the Frameworks/ directory of its app bundle, but it's not generating an .app bundle for some reason, so even when I change the Installation Directory and add a file copy step like described in this thread, since I don't have an app bundle, I don't have a Frameworks directory. So with no app bundle, this technique isn't working. (I'm not sure I even understand under what conditions an app bundle is generated or not.)

My project is here, for your reference. Can anyone help? I'm still getting my feet wet in Xcode and Mac OS X development in general, so apologies in advance if this question seems neophytic.

Community
  • 1
  • 1
NattyBumppo
  • 336
  • 2
  • 8

2 Answers2

2

This stuff is always hard to figure out in the beginning.

You don't need to copy the frameworks into the Frameworks directory because your project is a command line app. You only have an app bundle when you make a Cocoa app. And even in that case you only need to copy the frameworks into the Frameworks directory if you are going to distribute the app to people who don't have OpenCV installed.

Note that this is despite the fact that libopencv_core.2.3.dylib is included in the same directory as the executable.

Don't copy the library files into your executable file's folder. Just let it link to the files in /opt/local/lib

I downloaded your project and was able to get it to run. Here's what I did (using Xcode 4):

  1. Delete the dylib files (and links) from the Edge Display folder
  2. Delete the Copy Files build phase that copies the library files
  3. Delete the @executable_path/../Frameworks Installation Directory setting
  4. Delete everything from the OpenCV Frameworks folder in the Xcode project
  5. Select the target, select Build Phases, expand Link Binary With Libraries
  6. Click the plus button, click Add Other…, type "/opt/local/lib", select libopencv_core.dylib (note I didn't select a file with a version number)
  7. Repeat for libopencv_highgui.dylib and libopencv_imgproc.dylib (those are the only libraries your project needs)
  8. Set the Deployment Target to OS X 10.6 because that's what I'm using
  9. Click Run and get this:

My beautiful edge-detected face

My system is a little different than yours because my OpenCV libraries are installed in /usr/local/lib, but you already have /opt/local/include in the project's Header Search Path so you should be fine.

There are some instructions for using the OpenCV libraries in an OS X command-line project on the OpenCV Wiki but they're written for Xcode 3. I'll try to add some updated instructions for Xcode 4.

SSteve
  • 10,550
  • 5
  • 46
  • 72
  • Thanks a lot for your help! Correct me if I'm wrong, but if I add libraries from /opt/local/lib and then distribute the executable to someone without OpenCV installed there, won't it throw an error on being unable to find those libraries? Where's the setting located for directing it to look in the executable's directory or any other arbitrary directory? Thanks! – NattyBumppo Dec 16 '11 at 07:23
  • Yes. You are correct. But you have to make a distinction between command-line apps and Cocoa apps. With a Cocoa app you can embed the dylibs. You have to tell the application where to look for the libraries and you also have to tell the libraries where to look for themselves. [Details here.](http://stackoverflow.com/questions/7117128/xcode-dylib-looking-in-usr-lib/7170620#7170620). With a command-line app I believe you have to build and embed a static library the way you do with an iPhone program, but I haven't done that so I don't know the details. – SSteve Dec 16 '11 at 16:34
  • Hi guys, I know it may be too late but do you know if it is possible to include dynamic library using only .ipa (in case you don't have source code and you can't access project settings). Maybe to set some settings to info.plist? Thanks. – Mike.R May 27 '13 at 09:57
0

In your project settings you can use @executable_path in order to get to the right location. Hope that helps.

You could also try loading the library yourself from code I guess..

Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52