Using C libraries for a D program

From D Wiki
Revision as of 02:02, 3 December 2019 by D user (talk | contribs) (Category:HowTo)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

WARNING: This crude recipe by an absolute D newbie has not yet been tested--corrections are welcome!

Assume one has the following, error-free files:

  • a D language source program:
  • a C ABI shared library file:
  • a D language binding interface file to the C library API headers:

We first create a GNU Makefile to build program test-bu using those three files (note the leading spaces on lines following targets must be tabs in a real GNU Makefile):

$ cat Makefile
# use the dmd compiler
DMD = /usr/bin/dmd

LIBDIR = /usr/local/brlcad/lib
MODDIR = /usr/local/brlcad/include


PROG = test-bu

all: $(PROG)

$(PROG): main.o bu.o
        $(DMD) -of$@ main.o bu.o -L$(LIBDIR) -lbu
%.o: %.d
        $(DMD) -c $< -of$@

bu.o: $(BINDING)
        $(DMD) -c $< -of$@

        rm $(PROG) *.o

Then we can build test-bu by executing:

$ make test-bu

The GNU Makefile can be made more general and efficient, but it should produce the desired binary program.

Note also that a fully D-supporting C library installation would probably have the D binding interface available as a pre-compiled *.o file.