在ghci中导入inline-c模块

时间:2015-06-07 06:07:22

标签: c haskell

我想在ghci会话中导入一些inline-c生成的函数。所有源文件和目标文件都在src/子文件夹中,但我收到导入错误:

src/Main.hs:3:8:
    Could not find module ‘Test1’

我包含所有文件以供参考。提前致谢

Main.hs

module Main where

import Test1

main = 
  test1

以及要导入的功能

{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Test1 where

import qualified Language.C.Inline as C
import Foreign.C.Types

C.include "<stdio.h>"
C.include "<math.h>"

test1 = do
  s <- readAndSum n
  [C.exp| void{ printf( "Sum: %.2d\n", $(int s)) } |]
  putStrLn $ "Sum: " ++ show s

readAndSum n = 
  [C.block| int {
      int i, sum = 0, tmp;
      for (i = 0; i < $(int n); i++) {
        scanf("%d ", &tmp);
        sum += tmp;
      }
      return sum;
    } |]

生成文件:

main:
    ghc -c src/Test1.hs 
    cc -c src/Test1.c -o src/Test1_c.o
    ghci src/Main.hs src/Test1_c.o -isrc/ 
    # ghci src/Main.hs -Lsrc/ 

更新:我已经修复了makefile,现在编译了。然而,下一个重要问题是如何在每次调用中隔离效果,即以一致的方式重置值。

1 个答案:

答案 0 :(得分:0)

ghci调用的最小更改完成了这项工作:

ghci src/Main.hs src/Test1_c.o -isrc/ 

这意味着我们添加一个带有-i标志的包含路径来修复Could not find module错误,并明确链接从inline-c代码段生成的目标文件。

相关问题