15. Using Green Card

So, you probably wonder, how can I actually make use of GreenCard and its output with the Haskell system you work with? This section outlines the series of steps you need to follow, concentrating for the moment on the use of GreenCard together with either Hugs or GHC.

15.1. Running Green Card

If you downloaded GreenCard as part of a binary distribution, you should have a binary that you can invoke like other command-line tools, i.e., assuming you have written the following spec:

module GCTest where

import Foreign.StdDIS

%fun my_sin :: Double -> Double
%code res1=sin(arg1);

you can generate portable FFI-compatible Haskell stubs as follows:

  greencard -c GCTest.gc -o GCTest.hs --target=ffi 

or GHC-specific ones with

  greencard -c GCTest.gc -o GCTest.hs --target=ghc 

In the case you're working on a Win32 platform and downloaded the GreenCard installer, the -i... option isn't required.

15.2. Compiling Green Card stubs

Having generated some Haskell stubs, and in the case of the FFI target, some C code too, compiling these is the natural next step. If you're using GHC target, the following should do:

 ghc -c GCTest.hs -o GCTest.o -fglasgow-exts 
     -i/directory/where/you/put/Foreign/GreenCard.gc

Upon completion, it should have produced the object file, GCTest.o, which can then be used and linked into your application.

If you're using the FFI target, Green Card generates some stub files GCTest_stub_ffi.c and GCTest_stub_ffi.h which will need to be compiled and linked into your application. For example with Hugs, one uses ffihugs to compile and link the C code.

 ffihugs GCTest.hs +L"GCTest_stub_ffi.c" 
     -P/directory/where/you/put/Foreign.GreenCard:

When Hugs loads in the Green Card generated Haskell stubs, it looks for the stub files at the same time so they must be placed in the same directory as the generated Haskell stubs.

Of course, if your stubs interface to some external library, you'll no doubt need to augment with at least extra -lwhatever options to satisfy the demands of the linker.