Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bundle a dylib within a Mac app bundle in Qt?

I have an application and a dylib that it links against. It is my understanding that the application bundle should contain the dylib. Is that correct?

Is there a way to copy the dylib to the application bundle when built?

Thanks, Alan

like image 830
Alan Spark Avatar asked Jan 08 '23 07:01

Alan Spark


1 Answers

Is there a way to copy the dylib to the application bundle when built?

Well, sure -- with a little shell scripting, anything is possible. :)

In your build script, after you've called macdeployqt, but before you've done any code-signing, you'd copy the .dylib file over into the Contents/Frameworks inside your .app folder.

The trick after that is convincing the executable to look for the library in its new location rather than the location it was in when you linked the executable. (If you do a "otool -L ./MyProgram.app/Contents/MacOS/MyProgram" you will see the places where the program is looking for shared libraries, and you will see that it is not looking for your .dylib file in the Frameworks folder, yet)

To do that you use the install_name_tool command, e.g.:

install_name_tool -change /the/old/path/to/the_library_name.dylib  "@executable_path/../Frameworks/the_library_name.dylib" ./MyProgram.app/Contents/MacOS/MyProgram

After doing that you can run "otool -L" on your executable again to verify to your satisfaction that the executable is now set to look for the .dylib file inside the Frameworks folder.

like image 159
Jeremy Friesner Avatar answered Jan 16 '23 22:01

Jeremy Friesner