簡體   English   中英

使用Cabal和GHC建立圖書館的差異

[英]Differences in library building with using Cabal and GHC

我想從Haskell代碼構建庫,並在我的C ++項目中進一步使用這個庫(共享庫:dll左右)。

我找到了簡單的教程: http//blogging.makesmeanerd.com/?p = 367並成功重復這個例子。

此外,我簡化了這個例子,並獲得下一個代碼:

{-# LANGUAGE ForeignFunctionInterface #-}

module Grep where

import Foreign
import Foreign.C.String
import Data.Char

printCString :: CString -> IO ()
printCString s = do
    ss <- peekCString s
    putStrLn ss

getCStringFromKey :: IO CString
getCStringFromKey = do
    guess <- getLine
    newCString guess

foreign export ccall printCString :: CString -> IO ()
foreign export ccall getCStringFromKey :: IO CString

這是一個非常簡單的程序。 我輸入了下一個命令:

>ghc -c -O grep.hs
>ghc -shared -o grep.dll grep.o
Creating library file: grep.dll.a

之后,我有幾個文件:grep.dll,grep.dll.a和grep_stub.h(我的C ++項目的頭文件)。 我成功地在C ++項目中使用了這個庫。 C ++代碼非常簡單(我使用的是MS Visual Studio):

#include <iostream>
#include <string>
#include "grep_stub.h"

int main(int argc, char* argv[])
{
    std::string testStr;
    hs_init(&argc, &argv);
    HsPtr str1 = getCStringFromKey();
    std::cout << "We've get from Haskell: " << (char*)str1 << std::endl;

    HsPtr ss = "Hello from C++!";
    printCString(ss);

    std::cout << "Test application" << std::endl;
    std::cin.get();
    hs_exit();
    return 0;
}

編譯后,此代碼非常有效。

如果我使用Cabal構建系統構建相同的Haskell代碼(grep.hs):

name:                grep
version: 1.0
synopsis:            example shared library for C use
build-type:          Simple
cabal-version:       >=1.10

library
  default-language:    Haskell2010
  exposed-modules:     Grep
  extra-libraries:     HSrts-ghc7.6.3
  extensions: ForeignFunctionInterface 
  build-depends:       base >= 4

並運行Cabal構建系統:

>cabal configure --enable-shared
>cabal build
...
Creating library file: dist\build\libHSgrep-1.0-ghc7.6.3.dll.a

我有另一個dll(小尺寸),但我不能在MS VS中使用這個dll,因為我得到了很多鏈接器錯誤(如果我從Haskell平台獲得dll.a文件)。

主要問題:

  • 使用Cabal和ghc構建庫有什么區別?
  • 我如何使用GHC構建相同的dll,就像我使用GHC一樣?

您可以通過向庫設置添加ghc-options在cabal文件中設置其他選項。 不確定你的情況需要什么,但我遇到了同樣的問題(小的lib,鏈接器錯誤),對我來說,以下設置解決了它:

ghc-options: -staticlib

但我在Xcode的iOS項目中使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM