Dprogramming.com - The D Programming Language [archived content]
Directory

Home
News
Wiki
Entice Designer
DCode editor
Linkdef
bintod
Tutorial
D FAQ
Code
    bintod
    DFL GUI
    D irclib IRC
    fileprompt
    ini files
    Linkdef
    list linked list
    mtext
    Splat sockets
    trayicon
    wildcard
Contact
Paste
Links

linkdef 1.0

Linkdef is a small tool to help create lib files for C DLLs on Windows.

By piping the linker's output to linkdef and supplying which library to scan for functions, a def file will be generated with which you can create an import library lib file using Digital Mars' implib utility.

Download

Source and executable of linkdef.
DMC's Basic Utilities for implib.

Release notes

D functions in DLLs are not handled by linkdef.
Linkdef overrides the partial type safety provided in mangled names (be sure your prototypes are correct).
Lib files generated from linkdef def files will not be complete unless your program uses every function in the library, so you may not wish to copy them to the default lib directory.
Linkdef simply allows you to get by without a full lib file for the DLLs you are using. It will at least let you compile the program at hand without having to write def files by hand.

Usage

<link_output> | linkdef [<switches...>] <library_name>
link_output = Output of Digital Mars link. This can be from directly using link, through DMD, through executing a batch file or other type of script, or even from a file using the type command.
This is piped into linkdef using the | pipe character.
switches
   -n = Do not create backup def file if the target def file exists.
   -v = Verbose output.
library_name = Library name to check for functions. Do not include file name extensions.

Implib usage is available here.

Basic example

Say I have the following D source code:
import std.string;
import std.c.windows.windows;

extern(Windows) HINSTANCE ShellExecuteA(HWND, LPCTSTR,
	LPCTSTR, LPCTSTR, LPCTSTR, INT);

int main(char[][] args)
{
	if(args.length < 2) return 1;
	ShellExecuteA(null, "open", toStringz(args[1]), null, null, SW_SHOW);
	return 0;
}

Then I compile like so:

C:\d\exec>dmd code.d
c:\dmd\bin\..\..\dm\bin\link.exe code,,,user32+kernel32/noi;
OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

code.obj(code)
 Error 42: Symbol Undefined _ShellExecuteA@24
--- errorlevel 1

MSDN tells me ShellExecute is in shell32.dll, so run this information through linkdef on the command line:

C:\d\exec>dmd code.d | linkdef shell32
linkdef 1.0
Copyright 2005 Christopher E. Miller

c:\dmd\bin\..\..\dm\bin\link.exe code,,,user32+kernel32/noi;

OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

code.obj(code)
 Error 42: Symbol Undefined _ShellExecuteA@24
--- errorlevel 1

linkdef: 1 undefined symbols
linkdef: File shell32.def successfully created
linkdef: Use: implib shell32.lib shell32.def

Linkdef gave me a shell32.def file that I can pass to implib like so:

C:\d\exec>implib shell32.lib shell32.def
Digital Mars Import Library Manager Version 7.6B1n
Copyright (C) Digital Mars 2000.  All Rights Reserved.
Output is a Windows NT import library.
Digital Mars Import Library Creator complete.

Now compile it:

C:\d\exec>dmd code.d shell32.lib
c:\dmd\bin\..\..\dm\bin\link.exe code,,,shell32.lib+user32+kernel32/noi;

All set; use the program.

C:\d\exec>code www.dprogramming.com

Advanced example

You may have dozens of undefined symbols from several DLLs. You can go through all known libraries one at a time, adding the created ones to the command line. The linker will output less and less undefined symbols each time more are found and added to the command line. For example,
C:\d\supercool>dmd cool.d yours.lib their.lib | linkdef some
   ... Unable to find 13 undefined symbols ...
C:\d\supercool>implib some.lib some.def
   ...
C:\d\supercool>dmd cool.d yours.lib their.lib some.lib | linkdef next
   ... Unable to find 2 undefined symbols ...
C:\d\supercool>implib next.lib next.def
   ...
C:\d\supercool>dmd cool.d yours.lib their.lib some.lib next.lib | linkdef last
   ...
C:\d\supercool>implib last.lib last.def
   ...
C:\d\supercool>dmd cool.d yours.lib their.lib some.lib next.lib last.lib
   ...
Note: example trimmed for brevity.
As you can see, after finding symbols in a library, you can add the new lib file to the dmd command and check another library.
Copyright © 2004-2008 Christopher E. Miller