$ cd c-proj
c-proj$ ls
Main.hs c-proj.cabal c_file.c
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;
}
stack init
。$ stack init
<< Shell output snipped >>
$ stack build
<< Shell output snipped >>
$ stack exec main
2 + 10
12
executable main
下添加以下条目:extra-lib-dirs: "."
和extra-libraries: File.o
。 - AlecSetup.hs
文件污染我的项目! - Alec
stack build
并使用 FFI 进行编译吗? - user668074.cabal
文件中,而不是.stack
文件中。我已经发布了一个示例项目。 - Alec