次の最小限の例 (外部依存関係なし) をビルドして実行し
C->Haskell Compiler, version 0.25.2 Snowboundest, 31 Oct 2014
build platform is "x86_64-darwin" <1, True, True, 1>
ます。ビルド コマンド:
c2hs Structs.chs
ghci Structs.hs
しかし、リンカは
During interactive linking, GHCi couldn't find the following symbol: get_foo
Q : これはなぜですか?どうすれば修正できますか? 事前にThx
A : tl;dr : .c を .o オブジェクト コードにコンパイルしていませんでした。DUH!
コードは次のとおりです。
構造体.chs
module Main where
import Foreign
import Foreign.C
#include "Structs.h"
{#pointer *foo as Foo#}
{#fun get_foo {`Int'} -> `Foo' return* #}
main :: IO ()
main = do
foo <- get_foo 3
a1 <- {#get struct foo->a#} foo
print a1
構造体.h
struct foo {
int a;
};
struct foo *get_foo(int n);
構造体.c
#include "Structs.h"
struct foo f;
struct foo *get_foo(int n)
{
f.a = n;
return &f;
}
構造体.hs
-- -- C2HS generated Hs file, edited for clarity
module Main where
import Foreign
import Foreign.C
type Foo = Ptr ()
get_foo :: Int -> IO Foo
get_foo p =
get_foo'_ (fromIntegral p)
main :: IO ()
main = do
foo <- get_foo 3
a1 <- (\ptr -> peekByteOff ptr 0 :: IO CInt) foo
print a1
foreign import ccall safe "Structs.chs.h get_foo"
get_foo'_ :: CInt -> IO Foo
EDIT :ghc --make Structs.hs
同じリンカー エラーが発生します。
$ ghc --make Structs.hs
Linking Structs1a ...
Undefined symbols for architecture x86_64:
"_get_foo", referenced from:
_c2MX_info in Structs.o
_c2Or_info in Structs.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)