Can I include dylib-s in my executable?

(Yes, I understand that the whole point in dylib is that it loads dynamically, but I'm trying to create a standalone package.)

I have an executable that I created from the command line - on OS-X / Lion, if that matters. I delivered the executable to a friend, but it cannot start it because it does not have installed libraries. He would prefer not to install the libraries, so now I am trying to create a package that includes the source executable plus all the necessary libraries.

I'm used to working in the Xcode (IDE) and am not very familiar with the build and command line tools and their variants. (I built this tool following very good instructions from the Internet.) Therefore, explicit instructions would be helpful.

Thanks!

+6
source share
2 answers

You cannot directly place dylib inside an executable. (This is pretty much what was created for bundles, but it does not help you with command line tools.)

You can rebuild each dylib as a static (.a) library, in which case the entire executable will be copied to the executable, and you do not need to distribute anything with it. If you have a source for libraries, this is usually very simple, but without knowing exactly how you build things, it's hard to say what to change.

One thing to consider when statically binding is that it affects the sharing of different licenses. In particular, if any of the libraries that you use is LGPL licensed, their static link has consequences that do not have dynamic linking. See this question (and answer links) for more details, but in fact you should not trust the answers to Stack for legal advice on licenses. Anyway, this is probably not a problem for OP β€œI want to create a program and pass it on to my friend”, but for others who read this question later, it may be.

If static linking is not possible or desirable, all you have to do is combine the executable and dylib together and upload them to your friends machine. Since he apparently does not want to run the installer, this means tarball or zipfile.

The only tricky part is making sure exe knows where to find the dilibs. If each dylib is not in the dyld search path (see the man page for dyld for more details, but this will not help you if you do not want to install anything), or in the same place as at the time you associated with it, the launch of the executable file will fail with the error "image not found" from dyld.

Fortunately, "exact location" can mean a magic path, for example "@ executable_path / libfoo.dylib", which means "in the same directory as myexe", and not an absolute path, for example / opt / local / lib / libfoo .dylib "or relative path, for example" ../../foo/build/Release/libfoo.dylib ". (Note that normal relative paths refer to the current working directory, and not to the executable or package directory.)

You can see where myexe looks like by doing this:

otool -L myexe 

Everything that is not searched in @executable_path (except for files in / lib and / usr / lib, which are part of the OS and do not need to be distributed), you can fix this:

 install_name_tool -change ../../../mydl/build/Release/libmydl.dylib @executable_path/libmydl.dylib myexe 

Now you just need to copy all these dylib right next to myexe, pick it up and pass it to your friend, and he can just deploy it and run exe.

+10
source

On Mac OS X, dylib is usually stored in "/ usr / local /". By default, all applications look for any required dylib in "/ usr / local /".

Option 1 is to put dylib in your friends' machines "/ usr / local /".

Option 2, put dylib in the same directory as your executable.

Option 3, Correct the path in the application or dylib. Xcode dylib searching in / usr / lib will be helpful. You can use this option to pack dibs to your chosen location, that is, inside and outside the application package.

-1
source

Source: https://habr.com/ru/post/907404/


All Articles