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 to generate an iOS IPA file with Ionic Framework?
- iOS Multithreading – NSURLSession and UI updates
- Objectmapper get array of one item within JSON
- swift_dynamicCastClassUnconditional issues in swift
- Member operator '%' must have at least one argument of type 'ViewController’
- Arrays of Generics in Swift
- SWIFT – KVO in xcode 6 beta 6.. observeValueForKeyPath no longer called
- iOS 7 – Status bar overlaps the view
- How to use service soliciting with IOBluetooth/CoreBluetooth?
- Compare two dates
- Jailbroken iPhone – root privileges
- CollectionView background clearColor not working
- Can't find variable: Promise on Safari
- How to use NSNotification
- Possible to handle your own http URL schemes in iOS?