GHCi + FFI + global C variables

Simon Marlow marlowsd at gmail.com
Tue Dec 11 10:24:25 CET 2012


On 10/12/12 00:11, Nils wrote:
> I'm currently working with a C library that needs to use/modify global C
> variables, for example:
>
>     igraph_bool_t igraphhaskell_initialized = 0;
>
>     int igraphhaskell_initialize()
>     {
>       if (igraphhaskell_initialized != 0)
>       {
>         printf("C: Not initializing. igraphhaskell_initialized = %i\n",
> igraphhaskell_initialized);
>         return 1;
>       }
>       // initialization
>     }
>
> If I compile an example programm using this library everything works
> fine, but if I try to run the same program in GHCi it dies with the message
>
>     C: Not initializing. igraphhaskell_initialized = -907777
>
> The value (and apparently the adress of the global variable) is
> completly off, and I have no idea what is causing this or how to solve
> this issue and make the library GHCi-friendly. Is it possible to run
> this code in GHCi at all? Also, since it's a foreign library I obviously
> cannot just change the C code to simply not use any global variables at
> all.

Sounds like it could be this: http://hackage.haskell.org/trac/ghc/ticket/781

Compiling your program with -fPIC should fix it.

Cheers,
	Simon




More information about the Glasgow-haskell-users mailing list