Difference between revisions of "Using C libraries for a D program"

From D Wiki
Jump to: navigation, search
(Created page with "'''WARNING: This 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 p...")
 
(binding file needs to be compiled)
Line 1: Line 1:
'''WARNING:  This recipe by an absolute D newbie has not yet been tested--corrections are welcome!'''
+
'''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:
 
Assume one has the following, error-free files:
Line 21: Line 21:
 
MODDIR = /usr/local/brlcad/include
 
MODDIR = /usr/local/brlcad/include
  
BINDINGS = bu.d
+
BINDING = $(MODDIR)/bu.d
  
 
all: test-bu
 
all: test-bu
  
test-bu: main.o  
+
test-bu: main.o bu.o
         $(DMD) -o $@ -I$(MODDIR) $(BINDINGS) -L$(LIBDIR) -lbu
+
         $(DMD) -o $@ main.o bu.o -L$(LIBDIR) -lbu
 
   
 
   
 
%.o: %.d
 
%.o: %.d
 +
        $(DMD) -c $< -o $@
 +
 +
bu.o: #(BINDING)
 
         $(DMD) -c $< -o $@
 
         $(DMD) -c $< -o $@
  
Line 37: Line 40:
  
 
<pre>$ make test-bu</pre>
 
<pre>$ make test-bu</pre>
 +
 +
The GNU Makefile can be made more general and efficient, but it should produce the desired binary program.

Revision as of 18:25, 15 May 2014

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:
./main.d
  • a C ABI library file:
/usr/local/lib/libbu.so
  • a D language binding file to the C library API headers:
/usr/local/include/bu.d

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

BINDING = $(MODDIR)/bu.d

all: test-bu

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

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

clean:
        -rm *.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.