I was following some FFI tutorials and examples (here and here), and I was wondering what should change when using stack?
In the examples, the source C file is compiled to an object file using gcc -c -o termops.o termops.c
and included in the gcc compilation using ghc --make -main-is FfiEx -o ffi_ex FfiEx.hs termops.o
. How can the equivalent be accomplished using stack?
Here is as minimal an FFI C project as I could imagine.
$ cd c-proj
c-proj$ ls
Main.hs c-proj.cabal c_file.c
The contents of these files:
c-proj.cabal
: describes the
name: c-proj
version: 0.1.0.0
cabal-version: >= 1.22
build-type: Simple
executable main
main-is: Main.hs
build-depends: base >= 4.9
c-sources: c_file.c
Main.hs
: the only Haskell source file
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
foreign import ccall "plus_ten" plusTen :: Int -> IO Int
main = do
n <- plusTen 2
print n
c_file.c
: the C source file
#include<stdio.h>
int plus_ten(int n) {
printf("%d + 10\n", n);
return n + 10;
}
Then, if you want to use Stack, you can run stack init
.
$ stack init
<< Shell output snipped >>
$ stack build
<< Shell output snipped >>
$ stack exec main
2 + 10
12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With