Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link to D Libraries in a D program

I´m new to the D Programming Language and have a very simple problem.

I want to compile a D Script Library once and then use it in my other D projects.

In C I linked to the .lib files and created headers for them, but in D I don´t find things like that (are there even some sort of headers in D?)

I use D-IDE as my IDE and DMD2 as my compiler.

like image 822
Moritz Schöfl Avatar asked Apr 28 '12 17:04

Moritz Schöfl


People also ask

How to link library to gcc?

We tell gcc where the . a or . so files are by using: -L/path/to/library/code We also have to tell it which libraries to link with, either by explicitly linking in the . a file or specifying a library by name, for example: -lfftw3 to link with libfftw3.

How do I include a .so file in Makefile?

In addition, if you want to dynamically link libraries, you need to tell the linker where they are. -L/dir/containing -lc . If you don't want to set a LD_LIBRARY_PATH when executing, you'll need to set rpath , -Wl,--rpath=/path/containing . Points for pointing out what --rpath does.


2 Answers

Create StaticLib.d:

module StaticLib;

int func(int x)
{
    return x+1;
}

Compile it:

dmd -lib StaticLib.d -ofStaticLib.lib

Create App.d:

module App;
import std.stdio;
import StaticLib;

void main(string[] args)
{
    writeln("func(3) = ", StaticLib.func(3));
}

Create StaticLib.di (d header):

int func(int x);

Compile it:

dmd App.d StaticLib.di StaticLib.lib -ofApp.exe
like image 184
dnewbie Avatar answered Jan 03 '23 17:01

dnewbie


there are .di (D interface) files which can be used as header these can be generated from your sources with the -H compiler switch

however the libraries I've seen will just have the source files to import

you can use the -I switch to specify where the compiler will look for imports

and the -L switch will be passed to the linker

like image 20
ratchet freak Avatar answered Jan 03 '23 18:01

ratchet freak