Haskell FFI: How do you exchange C ++ collections?

I have a function that returns vector<MyClass> ; What's the best way to change this to something suitable for FFI?

I think a type like :: [CIntPointer] can be a good compromise if possible.

+7
source share
1 answer

You can define your own C functions for highlighting, freeing, pasting, deleting, etc. These functions can wrap the C ++ container that you want to access. For example:

 extern "C" { Obj * obj_create() { return new Obj(); } void obj_destroy(Obj * schema) { delete obj; obj = NULL; } ... ... } 

then declare them in FFI and wrap them in whatever way you want.

 data SomeObject type Obj = Ptr SomeObject foreign import ccall unsafe "obj_create" createObj :: IO Obj foreign import ccall unsafe "obj_destroy" destroyObj_ :: Obj -> IO () foreign import ccall unsafe "&obj_destroy" destroyObj :: FunPtr (Obj -> IO ()) 

Some Gotchas:

  • Make sure you compile C files using the C ++ compiler (g ++ instead of gcc). this ensures that stdC ++ files are loaded correctly.
  • Pass the library addresses (-L) and libs (-lboost *) for reference when compiling the / lib program on the haskell side
+3
source

All Articles