[Haskell-cafe] global variables for foreign C functions

Judah Jacobson judah.jacobson at gmail.com
Mon Dec 1 20:30:33 EST 2008


On Mon, Dec 1, 2008 at 4:55 PM, Evan Laforge <qdunkan at gmail.com> wrote:
> On Mon, Dec 1, 2008 at 4:39 PM, Andrea Rossato
> <mailing_list at istitutocolli.org> wrote:
>> Hello,
>>
>> I'm writing the bindings to a C library which uses, in some functions,
>> global variables.
>>
>> To make it clearer, those functions need a global variable to be
>> defined. A C program using my_function, one of the library functions,
>> would look like:
>
> I don't think you can use the FFI to declare symbols for C.  One
> not-so-pretty but effective way to do it is create a stub.c with the
> variables declared along with setting functions, then bind those
> functions like any other.

You can limit the size of that stub file using:

foreign import ccall "&progname" progname :: Ptr (Ptr CChar)

which lets you access that global variable and write the
getters/setters in Haskell rather than C.

-Judah


More information about the Haskell-Cafe mailing list