Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I link a dynamic library in Xcode?

I am currently developing a program in Qt and it uses the library libqextserialport.1.dylib.

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?

like image 766
Sam Avatar asked Mar 12 '09 01:03

Sam


People also ask

How are dynamic libraries linked?

Dynamic libraries are linked during the execution of the final executable. Only the name of the dynamic library is placed in the final executable. The actual linking happens during runtime, when both executable and library are placed in the main memory.

How do I create a dynamic library in Xcode?

Create C Dynamic Library in XcodeOn the "Welcome to Xcode" page, select "Create a new Xcode project". On the template page, select Framework & Libraries >> C/C++ Library. Click Next. On the options page, enter the name of static library.


2 Answers

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 ...

like image 111
north5 Avatar answered Oct 11 '22 23:10

north5


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.

like image 43
benzado Avatar answered Oct 12 '22 00:10

benzado