Skip to content
/ gohaskell Public

Example of calling Haskell Function from Go program (using C interface)

Notifications You must be signed in to change notification settings

rusq/gohaskell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calling Haskell From Go Example

I did it for the lulz.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Building

Updating the source paths

Make sure to update the libraries path in main.go on the line

// #cgo LDFLAGS: Safe.o -L <...> -lHSbase-4.14.3.0 -lHSinteger-gmp-1.0.3.0 -lHSghc-prim-0.6.1 -lHSrts -lCffi -liconv -lm -ldl

-l parameters should remain - they specify the required Haskell libraries for linking. You may need to update the libraries versions.

Easy (Makefile)

Run:

make

and it should do the job.

Manual build

ghc -c -O Safe.hs
go build

If something goes wrong

Most likely some libraries are missing or not specified in main.go.

To verify which libraries and paths are in use on your system, follow the steps below.

Paste this source to a file named test.c

#include <HsFFI.h>
#ifdef __GLASGOW_HASKELL__
#include "Safe_stub.h"
#endif
#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;
    hs_init(&argc, &argv);

    i = fibonacci_hs(42);
    printf("Fibonacci: %d\n", i);

    hs_exit();
    return 0;
}

Run:

ghc -c -O Safe.hs
ghc --make -no-hs-main -v -optc-O test.c Safe -o test

This will generate the compile and link commands for your version of C compiler. Check the output and use it as the guidance, and let the sun enlighten your treacherous path.

References

Based on the following materials:

About

Example of calling Haskell Function from Go program (using C interface)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published