Testing FFI Code (with “foreign import”s) with GHCi

不羁的心 提交于 2019-12-05 01:48:36

ghci will load any library so long as it's valid for your architecture and can be located on some path. On windows, pathnames with spaces used to cause problems, I don't know if they still do.

To load your own code in ghci, you need to first compile it, then tell ghci to load the output of that:

mybox$ gcc -c myprint.c
mybox$ ghci Myprint.hs myprint.o

*Main> main
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package bytestring-0.10.0.2 ... linking ... done.
Tempura is great!
*Main>

You could also compile the C files into a library and load that into ghci, but for just one file using an object file is quite convenient. If you want to create a library, a command like @Jonke suggested should work. On my system (OSX),

mybox$ gcc -shared -fPIC myprint.c -o libmyprint.dylib
mybox$ ghci -L. -lmyprint Foo.hs

On my system it also works to just use the library filepath as an argument, but I don't know if that's portable.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!