How to Use Dylib in MAC Os X (C++)

How can I link executable with dylib in Mac OS X command line?

You are dynamically linking libraries to compile your code, which needs to tell the system where to find the libraries both compile time and run time.

Right now, you are only telling the compiler to compile from libraries in the path specified by -L, but not telling the system where to find the libraries when executing it.

You probably need this:

export DYLD_LIBRARY_PATH=./lib:$DYLD_LIBRARY_PATH

where ./lib indicates where your libraries are.

Include a dylib into an OS X Framework

I found a solution :

  • As every dylib, we have to use install-name-tool to specify the install-paths.

  • We also have to codesign the framework and the contained dylib with our developer ID.

screenshot of the solution in Xcode

GCC: Linking a dylib on Mac Os

Add -lmemcached at the very end when compiling.

How to create a .dylib C extension on mac os x with distutils and/or setuptools?

Here's what worked for me (added lines in my setup.py):

if sys.platform == 'darwin':
from distutils import sysconfig
vars = sysconfig.get_config_vars()
vars['LDSHARED'] = vars['LDSHARED'].replace('-bundle', '-dynamiclib')

This configuration seems to be hard-wired in the module _sysconfigdata. It's also overridable using environment variables, so this works as well:

LDSHARED="cc -dynamiclib -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F." python setup.py install

MAC OSX dylib's and how to use them

As moshbear commented above, dylib files are how shared libraries are packaged on Mac OS X. To use such a shared library, you need to pass two switches to your compiler, -L and -l. The first adds the directory containing your dylib to the linker's library search path, and the second specifies the library to link against. Something like this, for a fictional libfoo.dylib that lives in /usr/mylibs:

clang code.c -L/usr/mylibs -lfoo


Related Topics



Leave a reply



Submit