Linking to libraries with D
torhu
no at spam.invalid
Fri Nov 16 05:03:08 PST 2007
Jarrod wrote:
> Well I know this sounds a rather mundane question to all you code buffs out
> there, but I'm wondering how I would use C libraries with D.
>
> I've already somewhat figured it out, but I guess I'm still confused a little
> because unlike C, D doesn't have header files, and unlike perl, D doesn't seem
> to 'add' your imported modules into your code.
>
> See, what I've done is I've converted the curl C headers to D, which leaves me
> with two .d files. Now at first, with my perl experience I expected to be able
> to just import the module, and the compiler would include the necessary
> 'header' code into the finished object. However this is not the case. I had to
> compile the 'header' into an object, and then link it. This is all well and
> good, but it feels.. rather 'unclean' I guess. I mean I expect I'll be using
> curl for multiple applications, so am I going to have to make a static
> library, just for my D applications to link to.. another library?
>
> I guess the question I'm getting at is, would there a better way to do this?
> Is there a way to make the compiler actually compile and include the code you
> import? I would rather not have to make a static library just to use code I'm
> importing from another library, but I also don't want to have to manually
> build and link the code I'm importing every time either. Am I missing
> something here?
>
> Any advice much appreciated,
> Jarrod.
If you've only translated the C headers to D, you need to link with the
actual C library too:
dmd yourapp.d curl.lib (or probably -L-lcurl on linux?)
The .d files work as both headers and implementation files, as needed.
If you have an app that contains two files, main.d and tools.d, and uses
the library stuff.lib, you compile and link like this:
dmd main.d tools.d stuff.lib
Lets assume that main.d imports tools.d. When the compiler compiles
main.d, it uses tools.d just as a header, to see that the correct
declarations are there. But unless you put it on the compiler's command
line, tools.d itself won't actually be compiled and linked.
You can use the bud tool from dsource.org/projects/build to automate
this. Then you only need to include the main source file, plus
libraries to get the same effect:
bud main.d stuff.lib
There's also a similar tool called dsss that linux users seem to prefer:
http://www.dsource.org/projects/dsss
More information about the Digitalmars-d
mailing list