global variables for foreign C functions

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: char progname[] = "a_program_name"; int main( int argc, char *argv[] ) { param p; my_function ( &p ) etc. I've been searching the ML, the wiki, the net, etc. without finding some examples on how such things are dealt with in Haskell - is it possible, BTW? If I import those functions without defining the global variable I get a linker error: /usr/lib/mylib.a(cfile.o): In function `my_function': cfile.c:(.text+0x510): undefined reference to `progname' I hope the issue is clear. Any help would be greatly appreciated. Andrea

On Mon, Dec 1, 2008 at 4:39 PM, Andrea Rossato
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.

On Mon, Dec 1, 2008 at 4:55 PM, Evan Laforge
On Mon, Dec 1, 2008 at 4:39 PM, Andrea Rossato
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

On Mon, Dec 01, 2008 at 05:30:33PM -0800, Judah Jacobson wrote:
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.
this solves my problems quite nicely indeed, but I still cannot figure how to write a setter function that actually works. That is to say, after: newCString "new_name" >>= poke progname this: putStrLn . show =<< peekCString =<< peek progname would return "new_name", but the library, which is using "progname" to produce some debugging messages, doesn't seem to get it correctly: the original bits are gone, but instead of "new_name" I get some garbage. Thanks to everyone for the interesting and useful hints. Andrea

On Mon, Dec 01, 2008 at 04:55:14PM -0800, Evan Laforge wrote:
On Mon, Dec 1, 2008 at 4:39 PM, Andrea Rossato
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.
Yes, it is unfortunate this is the case. my ForeignData proposal was meant to address this: http://hackage.haskell.org/trac/haskell-prime/wiki/ForeignData I am not entirely sure about the proposal as described, but I think something like it should be done. John -- John Meacham - ⑆repetae.net⑆john⑈

I would find a ForeignData extension incredibly helpful. This will be
crucial if Haskell ever wants to target out of the ordinary systems.
/jve
On Mon, Dec 1, 2008 at 8:38 PM, John Meacham
On Mon, Dec 01, 2008 at 04:55:14PM -0800, Evan Laforge wrote:
On Mon, Dec 1, 2008 at 4:39 PM, Andrea Rossato
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.
Yes, it is unfortunate this is the case. my ForeignData proposal was meant to address this:
http://hackage.haskell.org/trac/haskell-prime/wiki/ForeignData
I am not entirely sure about the proposal as described, but I think something like it should be done.
John
-- John Meacham - ⑆repetae.net⑆john⑈ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Andrea Rossato
-
Evan Laforge
-
John Meacham
-
John Van Enk
-
Judah Jacobson