How can I link a dynamic library in Xcode?
I build it and run in x-code and it spits back:
dyld: Library not loaded: libqextserialport.1.dylib Referenced from: /Users/samuelreh/Desktop/QtOpenCV/build/Debug/QtOpenCV.app/Contents/MacOS/QtOpenCV Reason: image not found
The library is located in /Users/samuelreh/Desktop/QtOpenCV/qextserialport/build/.
I can run my program by changing to the executable folder /Users/samuelreh/Desktop/QtOpenCV/build/Debug/QtOpenCV.app/Contents/MacOS/ and entering:
install_name_tool -change libqextserialport.1.dylib /Users/samuelreh/Desktop/QtOpenCV/qextserialport/build/libqextserialport.1.dylib QtOpenCV
I know there is probably many solutions besides this. Anybody know the best / most-elegant / easiest to do from x-code?
3 Solutions Collect From Internet About “How can I link a dynamic library in Xcode?”
If I understand your problem correctly, your app is building fine, no errors when linking, but when you try to launch it the library cannot be found.
That’s not surprising, since the dylib file is in some arbitrary directory not on the system path. You either need to copy it into
/usr/lib (probably not a good idea) or include it in the application bundle. The latter is probably the better approach.
I’ve never tried it, but apparently you need to use a Copy Files Build Phase to put the dylib inside your bundle and then configure Xcode so that your executable will know where to find it.
I’ve just done exactly this, with a Module Bundle project. This was being included into a larger project with a separate executable.
I added a “Copy dylibs to frameworks” step, which copied the dylibs to /Foobar.bundle/Contents/Frameworks/Foobar/. Then I added a Run Script Phase to run as the final step, to fix the install names of the dylibs in the executable:
install_name_tool -change libBobDylan.dylib @executable_path/../Plugins/Foobar.bundle/Contents/Frameworks/Foobar/libBobDylan.dylib path/to/build/product/Foobar.Bundle/Contents/MacOS/Foobar
Of course, libBobDylan.dylib also linked to libBillyIdol.dylib. So I had to add another Run Script Phase at the very start of the Target to fix the install names here:
install_name_tool -change libBillyIdol.dylib @executable_path/../Plugins/FooBar.bundle/Contents/Frameworks/Foobar/libBillyIdol.dylib local/path/to/libBobDylan.dylib
I had over a dozen of these to link against; I had to persuade the supplier of the dylibs to pad their header to accommodate my many install_name changes …
You just drag the library from the directory onto your Xcode project, best into resources but it does not matter (I believe). I tried with and without checking the ‘copy files into bundle’ and it work in both cases, not sure if you need to include it in bundle for deployment.
I’ve tested this with sqlite3 and cocoa application (cocoa-touch).
- How do I create and cancel unique UILocalNotification from a custom class?
- No Swift settings in XCode 5 project
- Using multiple storyboards with a TabBarController
- screen styling when virtual keyboard is active
- swift – validating UITextField
- need a way to securely communicate between Priviliged Helper Tool (installed using SMJobBless) and the application
- STRUCT Array To UserDefaults
- No member named 'begin' in namespace 'std'
- What does “Constrain to margins” mean in Interface Builder in Xcode 6.0.1
- Best option for supplying Quadtree GPS Data to an app?
- Is there a way to instantly generate an array filled with a range of values in Swift?
- Objective-C check if subviews of rotated UIViews intersect?
- Delete UITable sections dynamically with commitEditingStyle in Swift
- Attach a video in SKPSMTPMessage
- -didSelectRowAtIndexPath: not being called