我正在遵循一些FFI教程和示例(在此处和此处),我想知道使用堆栈时应该更改?
在示例中,源C文件使用gcc -c -o termops.o termops.c
编译为对象文件,并使用ghc --make -main-is FfiEx -o ffi_ex FfiEx.hs termops.o
包含在GCC编辑中。如何使用堆栈来完成等效的?
这是我想象的最小的ffi c项目。
$ cd c-proj
c-proj$ ls
Main.hs c-proj.cabal c_file.c
这些文件的内容:
c-proj.cabal
:描述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
:唯一的haskell源文件{-# LANGUAGE ForeignFunctionInterface #-} module Main where foreign import ccall "plus_ten" plusTen :: Int -> IO Int main = do n <- plusTen 2 print n
c_file.c
:c源文件#include<stdio.h> int plus_ten(int n) { printf("%d + 10n", n); return n + 10; }
然后,如果要使用堆栈,则可以运行stack init
。
$ stack init
<< Shell output snipped >>
$ stack build
<< Shell output snipped >>
$ stack exec main
2 + 10
12