作为概念的证明,我想将一些普通的Haskell代码链接到我的VisualC++应用程序中(使用Visual 2013)。使用GHC 7.8.3 32位构建,但是使用GHC 7.8.4 64位构建不工作(注意GHC版本中的细微差异)。
有3个文件:Grep.hs和StartEnd.c是用GHC构建的,以形成一个动态链接库。main.cpp是用Visual 2013构建的,并试图链接到DLL库中。
我正在根据以下内容构建DLL:
> ghc -shared -O -optc-O -o Grep.dll StartEnd.c Grep.hs在Visual中,我只需链接到Grep.dll.a并包含C:\Program Files\MinGHC-7.8.4\ghc-7.8.4\lib\include,但是链接失败
1>main.obj : error LNK2001: unresolved external symbol _HsEnd
1>main.obj : error LNK2001: unresolved external symbol _freegrep
1>main.obj : error LNK2001: unresolved external symbol _grep
1>main.obj : error LNK2001: unresolved external symbol _HsStart
1>C:\Code\Grep\dist\Win32\Release\Grep.exe : fatal error LNK1120: 4 unresolved externals当我用32位构建,而不是64位时,同样的过程起作用。我做错什么了?(当我试图链接64位库时,我正在构建一个64位应用程序。)
源文件:
Grep.hs
{-# LANGUAGE ForeignFunctionInterface #-}
module Grep where
import Foreign
import Foreign.C.String
import Data.List (isInfixOf)
filterlines f = unlines . filter f . lines
grep :: CString -> CString -> IO CString
grep i s = do
ii <- peekCString i
ss <- peekCString s
newCString $ (filterlines (isInfixOf ii)) ss
freegrep :: CString -> IO ()
freegrep s = free s
foreign export ccall grep :: CString -> CString -> IO CString
foreign export ccall freegrep :: CString -> IO ()StartEnd.c
#include <Rts.h>
void HsStart()
{
int argc = 1;
char* argv[] = {"ghcDll", NULL}; // argv must end with NULL
// Initialize Haskell runtime
char** args = argv;
hs_init(&argc, &args);
}
void HsEnd()
{
hs_exit();
}main.cpp
#include <HsFFI.h>
#include <Grep_stub.h>
#include <iostream>
extern "C" {
void HsStart();
void HsEnd();
}
int main(int argc, char* argv[])
{
HsStart();
HsPtr str;
str = grep("test", "This is a test\nwith many lines\nand it failed\nand the test passed");
if (str)
{
std::cout << (char*) str;
freegrep(str);
}
HsEnd();
return 0;
}发布于 2015-02-05 14:34:54
由于这些注释,我将实际的错误输出添加到post中。这让我注意到,Visual的混乱的配置管理接口再次咬了我一口。它仍然试图构建Win32版本,尽管我的配置设置是x64说的。长话短说,起作用了!
https://stackoverflow.com/questions/28335521
复制相似问题