Undefined references when linking to C library

Jonathan M Davis jmdavisProg at gmx.com
Wed Dec 22 20:07:33 PST 2010


On Wednesday 22 December 2010 19:25:35 Peter Federighi wrote:
> Hello all.
> 
> I'm writing a simple terminal game (that will eventually be turned into a
> simple SDL game) and thought I would add mouse support via libgpm.  So, I
> converted gpm.h into gpm.d.  Perhaps I didn't do this correctly because I
> get several undefined references when trying to link.
> 
> Here's an example:
> The original gpm.h says:
> extern int gpm_zerobased;
> extern unsigned char    _gpm_buf[];
> extern unsigned short * _gpm_arg;
> 
> My gpm.d says:
> extern int gpm_zerobased;
> extern char*  _gpm_buf;
> extern ushort* _gpm_arg;
> 
> When running 'dmd gev.d gpm.d -L-lgpm', I get:
> gpm.d:(.text._D3gpm15Gpm_DrawPointerFiiiZv+0x12): undefined reference to
> `_D3gpm8_gpm_bufPa'
> gpm.d:(.text._D3gpm15Gpm_DrawPointerFiiiZv+0x26): undefined reference to
> `_D3gpm13gpm_zerobasedi'
> gpm.d:(.text._D3gpm15Gpm_DrawPointerFiiiZv+0x34): undefined reference to
> `_D3gpm8_gpm_argPt'
> 
> Does anyone have any ideas?

Did you wrap the C declarations in an extern(C) block? Without that, it's going 
to think that your variables are D variables not C variables. The same goes for 
any functions - _especially_ for the functions. In fact, a large portion of - in 
not all of - your gpm.d file should likely be in extern(C).

You can try htod ( http://www.digitalmars.com/d/2.0/htod.html ) and see what it 
creates. It won't necessarily be correct, but it might be, and it might give you 
a better idea of where you screwed up. It's a Windows program, but it will run 
in wine.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list