我独自使用GHC成功复制了此示例。
https://wiki.haskell.org/Calling_Haskell_from_C
最终目标是在Haskell中编写我的程序的99%,然后从用C编写的事件循环中调用它:
#include <HsFFI.h>
#ifdef __GLASGOW_HASKELL__
#include "../.stack-work/dist/x86_64-linux/Cabal-2.4.0.1/build/Lib_stub.h"
extern void __stginit_Lib(void);
#endif
#include <stdio.h>
#include <time.h>
extern void hs_add_root (void (*init_root)(void));
int main(int argc, char *argv[])
{
int i;
hs_init(&argc, &argv);
#ifdef __GLASGOW_HASKELL__
hs_add_root(__stginit_Lib);
#endif
for (int m = 0; m < 10; ++m) {
i = fibonacci_hs(42);
printf("Fibonacci: %d\n", i);
}
hs_exit();
return 0;
}
在C语言中运行事件循环的动机是,据我所读,在Haskell中很难或不可能强制执行X次/秒的评估。
这是package.yaml:
name: c-loop
version: 0.1.0.0
github: "githubuser/c-loop"
license: BSD3
author: "Author name here"
maintainer: "example@example.com"
copyright: "2019 Author name here"
extra-source-files:
- README.md
- ChangeLog.md
# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/c-loop#readme>
dependencies:
- base >= 4.7 && < 5
library:
source-dirs: src
executables:
c-loop-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
- -fobject-code
# - -no-hs-main
- --make -no-hs-main -optc-O ./c/eventLoop.c Lib -o eventLoop
dependencies:
- c-loop
tests:
c-loop-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- c-loop
当我跑步时:
$ stack build
我明白了:
<no location info>: error: module ‘Lib’ cannot be found locally
有人知道发生了什么事吗?
答案 0 :(得分:3)
顺便说一句,您的动机似乎被误导了。我认为C事件循环的唯一目的是定期进行“强制评估”,这没有任何好处。您可以在Haskell中做到这一点。
上面的示例中出现的问题可能是Lib
中的ghc-options
。但是,您应该使用其他Cabal字段来代替,这会使事情更加顺利。
这是如何使您的最小示例与Stack一起使用。使用下面列出的四个文件创建一个新目录,然后运行stack build
,然后运行stack exec c-loop-exe
。
几点:
package.yaml
文件来执行此操作,但是您必须将Cabal语法转换过来。__stginit
和hs_add_root
垃圾。c-sources
),则无需为存根的路径进行硬编码。-opt-O2
标志。这是Stack的默认设置。四个文件的内容:
-- stack.yaml
resolver: lts-13.21
packages:
- .
-- c-loop.cabal
cabal-version: 1.12
name: c-loop
version: 0.1.0.0
build-type: Simple
executable c-loop-exe
main-is: src/Lib.hs
ghc-options: -no-hs-main
c-sources: c/eventLoop.c
build-depends:
base >=4.7 && <5
default-language: Haskell2010
-- c/eventLoop.c
#include <stdio.h>
#include <time.h>
#include "HsFFI.h"
#include "Lib_stub.h"
int main(int argc, char *argv[])
{
int i;
hs_init(&argc, &argv);
for (int m = 0; m < 10; ++m) {
i = fibonacci_hs(42);
printf("Fibonacci: %d\n", i);
}
hs_exit();
return 0;
}
-- src/Lib.hs
{-# LANGUAGE ForeignFunctionInterface #-}
module Lib 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