type Fun = CString -> IO CInt
foreign import ccall unsafe "dynamic" c_printf :: FunPtr Fun -> Fun
printf :: String -> IO Int
printf str = do
withDL "libc.so.6" [RTLD_NOW] $ \dl -> do
printf_ptr <- dlsym dl "printf"
let fun = c_printf printf_ptr
liftM fromIntegral $ withCString str fun
main = printf "hello world"
Problem: I don't understand how I can generate the foreign import
statements at runtime. I am currently considering generating strings
of such expressions, and using hs-plugins in order to compile them.
This seems like a complete hack though - I'm dynamically loading a
haskell library in order to dynamically load a foreign library.
Another option is to write a dynamic loader in C, and then call this
from haskell, but how would I type the loading function - obviously I
can give it a CString for the library, and another for the function in
question - but what would the return type be?
>> Problem: I don't understand how I can generate the foreign import
>> statements at runtime.
> there are special C libraries that doest it. one of them is libffi,
> another one (can't recall its name) is used by ghc itself. libffi
> isn't x64-compatible, unlike second one
Thanks a lot. Though the haskell wiki  claims that libffi works on x64.
> Thanks a lot. Though the haskell wiki  claims that libffi works on x64.
i don't know, just read yesterday on Lua list:
> A question for Fabio: what are the issues with Alien for 64-bit Windows?
I can answer part of that. Libffi  (the C library on top of which
Alien is built) has no support for 64-bit windows. More specifically
there is a need for some runtime generated glue code (mainly for
callbacks), and assembler support for x64 is very poor so far (many
Visual C++ compilers come without assembler and/or lack support for
Python and Java (in the JNA package) have their own port of libffi
specifically for x64. However I never managed to compile any of these
two for Alien (usually because of the lack of assembler).
I tried all this about a year ago, so things may have changed since then.