How to access hs_free_fun_ptr from a Visual Studio DLL

Brian Hulley brianh at
Mon Mar 27 06:44:03 EST 2006

Krasimir Angelov wrote:
> Hi Brian,
> The problem is that hs_free_fun_ptr is defined in a static library
> (the Haskell RTS) while you have declared it with
> __declspec(dllimport). In this case the compiler is tring tp optimize
> the call and it is using __imp__hs_free_fun_ptr instead of
> hs_free_fun_ptr. You have to remove the dllimport pragma and to link
> your program with the Haskell RTS library. Unfortunatelly the the
> static library format is different for VC++ and GCC and I expect that
> you may have problems if you try to statically link gcc code with VC++
> code.

Hi Krasimir,

Yes I was getting confused about the difference between static and dynamic 
libraries. I've mostly been using static libs in my C++ projects, and when 
you build a static lib it doesn't matter that some functions are not defined 
in it: no linking is done at this point, so the library can be created 
without needing to specify where any extern functions are actually defined. 
However when building a DLL, the linker is invoked and so it needs to know 
the location of any function not defined in the DLL itself. But since my DLL 
needs to be used by multiple Haskell applications, and since the RTS is 
built into the application code as opposed to being supplied by a separate 
DLL itself, there is no way to link the DLL against RTS API functions, even 
if the VC/GCC imcompatibilites could be overcome.

However there is no need for this anyway because of the simple workaround of 
just wrapping freeHaskellFunPtr in a FunPtr and passing this into the DLL, 
effectively doing dynamic linking of this function explicitly rather than 
relying on the low level OS-specific linking process to accomplish this.

Thanks, Brian. 

More information about the Glasgow-haskell-users mailing list