我无法弄清如何构建一个调用Haskell Foreign Function Interface的C SDL应用程序,我的主要代码是C语言编写的,以下是我的.cabal文件:
build-type: Simple
extra-source-files: README.md
cabal-version: >=1.10
library
exposed-modules: AI
other-extensions: ForeignFunctionInterface
build-depends: base >=4.9 && <4.10
hs-source-dirs: src/haskell
default-language: Haskell2010
ghc-options: -O2 -shared -fPIC -dynamic
extra-libraries: HSrts-ghc8.0.2
我按照链接中的说明操作,但没有成功(该链接是针对OSX而非Linux的)。 我使用以下方法成功编译了Haskell源码:
cabal install
但是我无法想出如何以这种方式构建C代码,使得Haskell可以被识别并导入到C中。 以下是我的C和Haskell源代码示例:
main.c:
#include <stdio.h>
#include "game.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_error.h>
#include "HsFFI.h" // include path not recognized
#include "AI_stub.h" // new! edited
int main( int argc, char** argv ) {
hs_init(&argc, &argv);
//HASKELL CALL
int i;
i = fibonacci_hs(42);
printf("Fibonacci: %d\n", i);
//END HASKELL CALL
initializeSdl();
window = createWindow(SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
renderer = createRenderer();
printf("Pre gameLoop\n");
play();
return 0;
}
AI.hs:
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module AI where
import Foreign.C.Types
fibonacci :: Int -> Int
fibonacci n = fibs !! n
where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
fibonacci_hs :: CInt -> CInt
fibonacci_hs = fromIntegral . fibonacci . fromIntegral
foreign export ccall fibonacci_hs :: CInt -> CInt
P.S:
- 我正在使用Ubuntu 18.04进行开发。
- GHC版本为8.0.2。
- Cabal版本为1.24.0.2。
main-is: AI.hs
而不是AI.h
。 - chi.c
文件,以及你给它什么参数? - Geraint Ballingersdl2-config --libs --cflags
-ggdb3 -O0 --std=c99 -lSDL2_image -lm -Wall - Yan.F