www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - using dylib with dmd

reply frostmind <frostmind yandex.ru> writes:
Greetings every one,

I desperatly searched net about how to compile a d2
program that uses dylib on mac os, but unfortunately no luck.

What I've been doing:

I have a C library compiled as (taken from http://developer.apple.com/):
gcc -dynamiclib -std=gnu99 Ratings.c -current_version 0.1
-compatibility_version 0.1 -
fvisibility=default -o libRatings.A.g
 --> so I have libRatings.A.dylib that my testapp will use.

I've converted Ratings.h header of the lib to Ratings.d file, all functions
enclosed in extern(C);

I created a test program: test_d_client.d

import Ratings;
import std.stdio;

void main() {
	writeln("Starting test");

	char[] value1 = "*".dup;
	addRating(value1.ptr);
	writeln("rating: %s", ratings());
}

And now, when I execute: dmd test_d_client.d I get following output:

Undefined symbols:
  "_addRating", referenced from:
      __Dmain in test_d_client.o
  "_ratings", referenced from:
      __Dmain in test_d_client.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
--- errorlevel 1

What I am doing wrong here?
Apr 22 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
frostmind Wrote:

 And now, when I execute: dmd test_d_client.d I get following output:
 
 Undefined symbols:
   "_addRating", referenced from:
       __Dmain in test_d_client.o
   "_ratings", referenced from:
       __Dmain in test_d_client.o
 ld: symbol(s) not found
 collect2: ld returned 1 exit status
 --- errorlevel 1
 
 What I am doing wrong here?

You need to tell the linker where your library is. You can just pass it to dmd on the commandline.
Apr 22 2011
parent reply frostmind <frostmind yandex.ru> writes:
Thank you for your response! Hopefully I've done it right.

Now when everything is within the same folder,
and I execute:
dmd test_d_client.d -L.

(so I'm telling to look for libs in current dir)
Response is now different:

ld: in ., can't map file, errno=22
collect2: ld returned 1 exit status
--- errorlevel 1

What else could be done here to resolve it?
Apr 22 2011
parent Mike Wey <mike-wey example.com> writes:
On 04/22/2011 10:18 PM, frostmind wrote:
 Thank you for your response! Hopefully I've done it right.

 Now when everything is within the same folder,
 and I execute:
 dmd test_d_client.d -L.

 (so I'm telling to look for libs in current dir)
 Response is now different:

 ld: in ., can't map file, errno=22
 collect2: ld returned 1 exit status
 --- errorlevel 1

 What else could be done here to resolve it?

Asuming your lib is named libRatings an is in the current directory, this should work: dmd test_d_client.d -L-L. -L-lRatings -- Mike Wey
Apr 23 2011