How to compile a resource into a binary in Haskell?

后端 未结 4 1848
小蘑菇
小蘑菇 2020-12-29 04:12

Say I have a dictionary.txt file, I need to read it into a map and use it in my program, how can I make this dictionary.txt file contained in the compiled exe f

4条回答
  •  星月不相逢
    2020-12-29 05:08

    You can use a custom Makefile/Setup.hs hook and invoke windres (if you're on Windows) or objcopy/elfrc (if you're on Linux) to compile resources to COFF/ELF objects which you can then combine with your Haskell object files to form the final executable. You can then access resources using Haskell FFI like this (not tested):

    -- We have an image resource called "_imgdata"
    foreign import ccall "&" _imgdata :: CString 
    
    -- Size of _imgdata is 405585 bytes.
    imgdata :: CStringLen
    imgdata = (_imgdata, 405585)
    

    This solution will be more efficient than using file-embed (no CString -> ByteString conversions going on), but also more involved.

    As an aside, I also ended up needing resource file support during my work on cabal-install, so it will probably be integrated into some future version of Cabal (if I'll implement it).

提交回复
热议问题