www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Linking to Dynamic Library on Mac OS X

reply "TJB" <broughtj gmail.com> writes:
I have built a toy dynamic shared library on Mac OS X (in C), and 
I have verified that it works from C. Now I would like to call it 
from D. So I have created the following interface file:

$ cat hello.di
extern (C):
     void printHelloWorld();

which I try to compile and run. But I get the following error:

$ dmd main.d -L-lhello
ld: library not found for -lhello
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)
--- errorlevel 1

I gather that mac os x doesn't know where to find libhello.dylib 
(it is in the current directory). So how do I pass that 
information?

Thanks!
TJB
May 14 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 15 May 2015 at 03:33:47 UTC, TJB wrote:
 I have built a toy dynamic shared library on Mac OS X (in C), 
 and I have verified that it works from C. Now I would like to 
 call it from D. So I have created the following interface file:

 $ cat hello.di
 extern (C):
     void printHelloWorld();

 which I try to compile and run. But I get the following error:

 $ dmd main.d -L-lhello
 ld: library not found for -lhello
 clang: error: linker command failed with exit code 1 (use -v to 
 see invocation)
 --- errorlevel 1

 I gather that mac os x doesn't know where to find 
 libhello.dylib (it is in the current directory). So how do I 
 pass that information?

 Thanks!
 TJB
Off the top of my head: does adding -L-L$(pwd) help?
May 15 2015
parent reply "TJB" <broughtj gmail.com> writes:
 Off the top of my head: does adding -L-L$(pwd) help?
This is what I get: $ dmd main.d -L-L$(pwd) -lhello Error: unrecognized switch '-lhello' Sorry if this is completely elementary and I am being quite dumb. Thanks, TJB
May 15 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 15 May 2015 at 19:39:53 UTC, TJB wrote:
 Off the top of my head: does adding -L-L$(pwd) help?
This is what I get: $ dmd main.d -L-L$(pwd) -lhello Error: unrecognized switch '-lhello' Sorry if this is completely elementary and I am being quite dumb. Thanks, TJB
should be $ dmd main.d -L-L$(pwd) -L-lhello The -L means "send the following argument to the linker". Note that you may also find you need to help OS X find the dylib when running the program, either by moving it to one of the system locations or using DYLD_LIBRARY_PATH or DYLD_FALLBACK_LIBRARY_PATH (see http://stackoverflow.com/questions/3146274/is-it-ok-to-use-dyld-library-path-on-mac-os-x-and-whats-the dynamic-library-s). However, I think the runtime linker looks in the present directory by default, so you might get away with it.
May 15 2015
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2015-05-15 21:49, John Colvin wrote:

 Note that you may also find you need to help OS X find the dylib when running
the
 program, either by moving it to one of the system locations or using
 DYLD_LIBRARY_PATH or DYLD_FALLBACK_LIBRARY_PATH
That should not be necessary. -- /Jacob Carlborg
May 16 2015
prev sibling parent "TJB" <broughtj gmail.com> writes:
On Friday, 15 May 2015 at 19:49:30 UTC, John Colvin wrote:
 On Friday, 15 May 2015 at 19:39:53 UTC, TJB wrote:
 Off the top of my head: does adding -L-L$(pwd) help?
This is what I get: $ dmd main.d -L-L$(pwd) -lhello Error: unrecognized switch '-lhello' Sorry if this is completely elementary and I am being quite dumb. Thanks, TJB
should be $ dmd main.d -L-L$(pwd) -L-lhello
This works perfectly. A big thanks!
May 16 2015