Skip to content

Can't dynamically link with GHC API

Summary

I am trying to use the GHC API to dynamically link a Haskell module into my application. I have included this module in a package which is compiled with the -dynamic flag. My application runs GHC using the API, with the following procedure:

  1. Set the DynFlags to make GHC use in-memory linking
  2. Initialise the dynamic linker, with initDynLinker
  3. Look up the module in the unit database
  4. Link the package that the module is found in, with linkPackages
  5. Use getHValue to get a value from the module

However, when the module is loaded in getHValue, I get the following error message:

Bad interface file: ...
    mismatched interface file ways (wanted "", got "dyn")

This seems to mean that the dynamic linker is not expecting the package to be compiled dynamically, despite what seems to be all the relevant flags being set.

I have added debugging information to the code, and it seems that the module, with dynamic libraries, is found in the unit database. It also seems that linkPackages does link the package, although the package appears under Pkgs rather than Objs.

Steps to reproduce

A simple reproduction, with the output, can be found at https://github.com/jonathanjameswatson/dynlink.

Expected behavior

I would like the module to be successfully dynamically linked, so getHValue does not fail.

Environment

  • GHC version used: 9.0.2

Optional:

  • Operating System: Ubuntu 20.04.4 LTS on WSL2 for Windows 11
  • System Architecture: AMD64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information