https://github.com/rusq/gohaskell
Example of calling Haskell Function from Go program (using C interface)
https://github.com/rusq/gohaskell
c example golang haskell interface lulz tutorial
Last synced: 9 months ago
JSON representation
Example of calling Haskell Function from Go program (using C interface)
- Host: GitHub
- URL: https://github.com/rusq/gohaskell
- Owner: rusq
- Created: 2021-09-30T09:44:03.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-09-30T09:51:42.000Z (over 4 years ago)
- Last Synced: 2025-04-15T18:08:38.432Z (9 months ago)
- Topics: c, example, golang, haskell, interface, lulz, tutorial
- Language: Go
- Homepage:
- Size: 2.93 KB
- Stars: 12
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Calling Haskell From Go Example
> I did it for the lulz.

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`
```c
#include
#ifdef __GLASGOW_HASKELL__
#include "Safe_stub.h"
#endif
#include
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:
```sh
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:
* [Calling Haskell From C](https://wiki.haskell.org/Calling_Haskell_from_C)
* [Go: passing argv to C function](https://stackoverflow.com/questions/37657326/go-passing-argv-to-c-function)