简体   繁体   English

使用Cabal和GHC建立图书馆的差异

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

I'd like to build library from Haskell code, and further use this library (shared library: dll or so) in my C++ project. 我想从Haskell代码构建库,并在我的C ++项目中进一步使用这个库(共享库:dll左右)。

I found simple tutorial: http://blogging.makesmeanerd.com/?p=367 And successfull repeat this example. 我找到了简单的教程: http//blogging.makesmeanerd.com/?p = 367并成功重复这个例子。

Further, I simplify this example, and get next code: 此外,我简化了这个例子,并获得下一个代码:

{-# 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

It's very simple program. 这是一个非常简单的程序。 I typed next commands: 我输入了下一个命令:

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

After, I have a few files: grep.dll, grep.dll.a and grep_stub.h (header file for my C++ project). 之后,我有几个文件:grep.dll,grep.dll.a和grep_stub.h(我的C ++项目的头文件)。 I successfull use this library in C++ project. 我成功地在C ++项目中使用了这个库。 C++ code is very simple (I used MS Visual Studio): 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;
}

After compilation, this code works very well. 编译后,此代码非常有效。

If I build same Haskell code (grep.hs) with using Cabal build system: 如果我使用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

And run Cabal build system: 并运行Cabal构建系统:

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

I got another dll (with small size), but I can't use this dll in MS VS, because I get a lot of linker errors (ever if I get dll.a files from Haskell Platform). 我有另一个dll(小尺寸),但我不能在MS VS中使用这个dll,因为我得到了很多链接器错误(如果我从Haskell平台获得dll.a文件)。

Main questions: 主要问题:

  • What is difference between build library with Cabal and ghc? 使用Cabal和ghc构建库有什么区别?
  • How can I build the same dll with Cabal, as I get with GHC? 我如何使用GHC构建相同的dll,就像我使用GHC一样?

You can set additional options in the cabal file by adding ghc-options to your library settings. 您可以通过向库设置添加ghc-options在cabal文件中设置其他选项。 Not sure what is needed in your case, but I have run into the same issue (small lib, linker errors) and for me the following setting solved it: 不确定你的情况需要什么,但我遇到了同样的问题(小的lib,链接器错误),对我来说,以下设置解决了它:

ghc-options: -staticlib

But I use that in an iOS project in Xcode. 但我在Xcode的iOS项目中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM