Share via


Dynamic libraries on Windows, Linux and OSX

Dynamic libraries also known as shared libraries allow the same library to be used by multiple programs at the same time.  Dynamic libraries work slightly different on various platforms.  Here’s a quick summary of how they work on the three major platforms.  Both OSX and Linux can have hard coded search path in their binary call rpath.

MACs: very flexible.

So you can have an application reference a dynamic library in relative rpath, absolute rpath, or use the ENV called DYLD_LIBRARY_PATH.

MAC app is basically a folder structured like that. You are essentially double clicking on a folder.

The relative rpath referencing on OSX is great, as you can make your application more portable.

An example:

MyApp

---- libs // dylibs you want to distribute with your application.

---- bin // where EXE lives, you can edit the EXE to reference  ../dylibs/mylibrary.dylib

----resources // anything else.. images, data.

Meta-file // tells me what’s in the application bundle.

As you can see, a mac application can be self contained.

 

LINUX: Difficult distribute binary

Only allows absolute rpath or using LD_LIBRARY_PATH env variable. You can also use ldconfig to add your system wide global search path, or in /etc/ld.so.conf

Correction:   Linux does allow relative rpath, my memory of it only allowing absolute rpath may have been from a libtool bug I was experiencing, or an older versions of Linux linkers.

gcc test.c -Wl,-rpath=..
ldd a.out
        linux-vdso.so.1 =>  (0x00007fff84ffe000)
        libc.so.6 => ../libc.so.6 (0x00007fa37c8df000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fa37cc41000)

In order to combat different distros of Linux, libraries available, during an installation.  I tend to just statically link in everything in my application for compatibility.  That way, the application runs regardless.  Skype, apparently does that too.

 

Windows: system & current directories

1. The directory where the executable module for the current process is located.
2. The current directory.
3. The Windows system directory.
4. The Windows directory.
5. The directories listed in the PATH environment variable.

 

WHICH DYNAMIC LIBRARY DID YOUR APPLICATION JUST LOAD

If your application can't find the dynamic library specified by rpath, it'll look for them in DYLD_LIBRARY_PATH(MAC) or LD_LIBRARY_PATH. To find out where your application found their shared library dependencies:

MACS:  otool -L  to see which dylib your application will load.

LINUX:  ldd

Windows:  process explorer; don't develop with this tool.