[Haskell-cafe] Re: Resolved: ffi linking problem

Stefan O'Rear stefanor at cox.net
Fri Jun 1 23:02:14 EDT 2007


On Fri, Jun 01, 2007 at 10:48:12PM -0400, jeff p wrote:
> Hello,
> 
> >No, it sounds like you're using the wrong import syntax.
> >
> >That linker warning is a dead givaway you should be using ccall, not
> >stdcall.
> >
> Ok. I just tried changing this and now things work fairly well.
> I thought stdcall was the correct syntax for windows.
> 
> This seems like a strange state of affairs. I have a C library I want
> to use. I can create an executable which links directly to the library
> by using stdcall and -fvia-C, but I can't get ghci to use anything
> which links directly to the library. Or, I can indirectly link to the
> library via my own C wrapper functions using ccall, no need for
> -fvia-C, and get either a standalone or a module which ghci will use.

Actually it makes sense if you know a few of the subtle details.

The C compiler, because it parses header files, will not be confused by
an incorrect calling convention, in the special case of C.  You should
not rely on this behavior!

ccall refers to the standard C calling convention, on any platform.  The
Windows API functions use a non-standard calling convention, confusingly
named stdcall by Microsoft; GHC only follows established bad
terminology.

GHCi and -fasm do NOT use the C compiler, and require fully matched
signatures.

C wrapper functions are not needed to link to external ccall or stdcall
library functions.  They are only needed for weird calling conventions
(like C++) and macros.

Attempting to call a ccall function with a stdcall calling sequence, or
conversely, will probably cause a crash with modern compilers.  It may
have worked on very old non-optimizing compilers, but the defaulting
thing in your linker should have been removed long ago.

Stefan


More information about the Haskell-Cafe mailing list