Haskell Stack和C库

编程入门 行业动态 更新时间:2024-10-14 06:22:51
本文介绍了Haskell Stack和C库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这可能是这篇文章的副本。但唯一的答案似乎并不适合我,所以我在这里张贴我自己的案例,希望找到一个具体的解决方案。我在Linux Ubuntu Trusty上。

我有一个专有的C库libMyLib.so,位于/ usr / local / lib(包含在LD_LIBRARY_PATH中)。

我把这个haskell绑定到这个C库写成了美国式的方式。 Main.hs:

{ - #LANGUAGE ForeignFunctionInterface# - } 模块Main其中 导入外部导入Foreign.C.Types 外部导入ccall安全mycfunctionc_myCfunction :: CInt - > IO(CInt) - etc ... main = do - ...

我可以使用ghci测试这个程序,并将它传递给库和它可以找到它的目录: ghci / usr / local / lib / -lMyLib ,它工作得很好。

现在,我独立地尝试开始使用堆栈,并遵循本指南我能够创建几个非平凡的程序示例。

然而,我不能堆叠构建上面的FFI Main.hs程序,因为它抱怨缺少C库:

首先尝试: me @ user:〜/ myProject $ stack build

myProject-0.1.0.0:configure 配置myProject-0.1.0.0 ... myProject-0.1.0.0: build 预处理库myProject-0.1.0.0 ... 就地注册myProject-0.1.0.0 ... 为myProject-0.1.0.0预处理可执行文件'myProject-exe'.. 。链接.stack-work / dist / x86_64-linux / Cabal-1.22.5.0 / build / myProject-exe / myProject-exe ... .stack-work / dist / x86_64-linux / Cabal-1.22.5.0 / build / myProject-exe / myProject-exe-tmp / MyModule.o:在函数`c8QI_info'中:(.text + 0x102b):对'MyModule_mycfunction'的未定义引用 collect2 :错误:ld返回1退出状态 - 使用以下命令构建软件包myProject-0.1.0.0: /home/me/.stack/setup-exe-cache/x86_64-linux/设置,简单-Cabal-1.22.5.0-ghc-7.10.3 --builddir = .stack-work / dist / x86_64-linux / Cabal-1.22.5.0 build lib:myProject exe:myProject-exe --ghc -options-ddump- hi -ddump-to-file进程退出时带有代码:ExitFailure 1

第二次尝试,我添加了以下内容: - 在stack.yaml中添加了 extra-lib-dirs:[/ usr / local / lib] 文件。 - 在stack.yaml文件中添加了 extra-include-dirs:[/ usr / local / lib] (尽管可能不需要)。 - 在标签可执行文件或库下的项目cabal文件中添加了 extra-libraries:lMyLib (我也试过 extra-libraries:libMyLib.so 具有相同的结果)。

第二次尝试: me @ user:〜/ myProject $ stack build code $ $ $ $ $ $ $ $ $ $ $ $ $ $配置myProject-0.1.0.0 ... $ myProject-0.1.0.0: b $ b setup-Simple-Cabal-1.22.5.0-ghc-7.10.3:缺少对外部库的依赖: *缺少C库:lMyLib 这个问题通常可以解决通过安装提供此库的系统包(您可能需要-dev版本)。如果库已经安装了,但在非标准位置,那么您可以使用标志 --extra-include-dirs =和--extra-lib-dirs =来指定它的位置。 - 使用以下命令构建包myProject-0.1.0.0: /home/me/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22。 5.0-ghc-7.10.3 --builddir = .stack-work / dist / x86_64-linux / Cabal-1.22.5.0 configure --with-ghc = / home / me / .stack / programs / x86_64-linux / ghc- 7.10.3 / bin / ghc --with-ghc-pkg = / home / me / .stack / programs / x86_64-linux / ghc-7.10.3 / bin / ghc-pkg --user --package-db = clear --package-db = global --package-db = / home / me / .stack / snapshots / x86_64 -linl / lts-4.0 / 7.10.3 / pkgdb --package-db = / home / me / myProject /。 stack-work / install / x86_64-linux / lts-4.0 / 7.10.3 / pkgdb --libdir = / home / me / myProject / .stack-work / install / x86_64-linux / lts-4.0 / 7.10.3 / lib --bindir = / home / me / myProject / .stack-work / install / x86_64-linux / lts-4.0 / 7.10.3 / bin --datadir = / home / me / myProject / .stack-work / install / x86_64 -linux / lts-4.0 / 7.10.3 / share --libexecdir = / home / me / myProject / .stack-work / install / x86_64 -linux / lts-4.0 / 7.10.3 / libexec --sysconfdir = / home / me / myProject / .stack-work / install / x86_64-linux / lts-4.0 / 7.10.3 / etc --docdir = / ho me / me / myProject / .stack-work / install / x86_64-linux / lts-4.0 / 7.10.3 / doc / myProject-0.1.0.0 --htmldir = / home / me / myProject / .stack-work / install / x86_64-linux / lts-4.0 / 7.10.3 / doc / myProject-0.1.0.0 --haddockdir = / home / me / myProject / .stack-work / install / x86_64-linux / lts-4.0 / 7.10.3 / doc /myProject-0.1.0.0 --dependency = base = base-4.8.2.0-0d6d1084fbc041e1cded9228e80e264d --dependency = bytestring = bytestring-0.10.6.0-9a873bcf33d6ce2fd2698ce69e2c1c66 --dependency = safe = safe-0.3.9-e3aa437cf6afe091d2ac3ab91bc10ddd --dependency = split = split-0.2.2-10f39ee49f650eefddf38af51b65d10a --extra-include-dirs = / usr / local / lib --extra-lib-dirs = / usr / local / lib --enable-tests --enable-benchmarks 进程退出代码:ExitFailure 1

现在它知道它需要哪个C库,因为它抱怨它,而正确的额外库目录已添加(请参阅错误消息的最后)。我不确定注释(您可能需要-dev版本)是否相关。

我的问题:我被困在这里。还有什么我应该做的yaml或cabal文件或其他地方,以便 stack build 找到C库?

解决方案

extra-libraries 应该只有基本库名称,所以使用 extra-libraries :MyLib 。

This is possibly a duplicate of this post. But the only answer it got does not seem to work for me, so I post my own case here hoping to find a specific solution. I am on Linux Ubuntu Trusty.

I have a proprietary C library, libMyLib.so, located in /usr/local/lib (which is included in LD_LIBRARY_PATH).

I wrote the haskell bindings to this C library the ususal way. Main.hs :

{-# LANGUAGE ForeignFunctionInterface #-} module Main where import Foreign import Foreign.C.Types foreign import ccall safe "mycfunction" c_myCfunction :: CInt -> IO (CInt) -- etc... main = do -- ...

I can test this program with ghci, passing it the library and the directory where it can find it : ghci /usr/local/lib/ -lMyLib and it works just fine.

Now, independently, I am trying to start using stack, and following this guide I was able to build several non-trivial program examples.

However, I am not able to stack-build the FFI Main.hs program above as it complains about the missing C library :

First try : me@user:~/myProject$ stack build

myProject-0.1.0.0: configure Configuring myProject-0.1.0.0... myProject-0.1.0.0: build Preprocessing library myProject-0.1.0.0... In-place registering myProject-0.1.0.0... Preprocessing executable 'myProject-exe' for myProject-0.1.0.0... Linking .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/myProject-exe/myProject-exe ... .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/myProject-exe/myProject-exe-tmp/MyModule.o: In function `c8QI_info': (.text+0x102b): undefined reference to `MyModule_mycfunction' collect2: error: ld returned 1 exit status -- While building package myProject-0.1.0.0 using: /home/me/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build lib:myProject exe:myProject-exe --ghc-options " -ddump-hi -ddump-to-file" Process exited with code: ExitFailure 1

Before the 2nd try, I did add the following : - added extra-lib-dirs: [/usr/local/lib] to the stack.yaml file. - added extra-include-dirs: [/usr/local/lib] to the stack.yaml file (although it's probably not needed). - added extra-libraries: lMyLib in the project's cabal file under the labels executable or library (I also tried extra-libraries: libMyLib.so witht the same result).

Second try : me@user:~/myProject$ stack build

myProject-0.1.0.0: configure Configuring myProject-0.1.0.0... setup-Simple-Cabal-1.22.5.0-ghc-7.10.3: Missing dependency on a foreign library: * Missing C library: lMyLib This problem can usually be solved by installing the system package that provides this library (you may need the "-dev" version). If the library is already installed but in a non-standard location then you can use the flags --extra-include-dirs= and --extra-lib-dirs= to specify where it is. -- While building package myProject-0.1.0.0 using: /home/me/.stack/setup-exe-cache/x86_64-linux/setup-Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 configure --with-ghc=/home/me/.stack/programs/x86_64-linux/ghc-7.10.3/bin/ghc --with-ghc-pkg=/home/me/.stack/programs/x86_64-linux/ghc-7.10.3/bin/ghc-pkg --user --package-db=clear --package-db=global --package-db=/home/me/.stack/snapshots/x86_64-linux/lts-4.0/7.10.3/pkgdb --package-db=/home/me/myProject/.stack-work/install/x86_64-linux/lts-4.0/7.10.3/pkgdb --libdir=/home/me/myProject/.stack-work/install/x86_64-linux/lts-4.0/7.10.3/lib --bindir=/home/me/myProject/.stack-work/install/x86_64-linux/lts-4.0/7.10.3/bin --datadir=/home/me/myProject/.stack-work/install/x86_64-linux/lts-4.0/7.10.3/share --libexecdir=/home/me/myProject/.stack-work/install/x86_64-linux/lts-4.0/7.10.3/libexec --sysconfdir=/home/me/myProject/.stack-work/install/x86_64-linux/lts-4.0/7.10.3/etc --docdir=/home/me/myProject/.stack-work/install/x86_64-linux/lts-4.0/7.10.3/doc/myProject-0.1.0.0 --htmldir=/home/me/myProject/.stack-work/install/x86_64-linux/lts-4.0/7.10.3/doc/myProject-0.1.0.0 --haddockdir=/home/me/myProject/.stack-work/install/x86_64-linux/lts-4.0/7.10.3/doc/myProject-0.1.0.0 --dependency=base=base-4.8.2.0-0d6d1084fbc041e1cded9228e80e264d --dependency=bytestring=bytestring-0.10.6.0-9a873bcf33d6ce2fd2698ce69e2c1c66 --dependency=safe=safe-0.3.9-e3aa437cf6afe091d2ac3ab91bc10ddd --dependency=split=split-0.2.2-10f39ee49f650eefddf38af51b65d10a --extra-include-dirs=/usr/local/lib --extra-lib-dirs=/usr/local/lib --enable-tests --enable-benchmarks Process exited with code: ExitFailure 1

So now it knows which C library it needs, since it complained about it, while the right extra library directory has been added (see the very end of the error message). I am not sure if the comment (you may need the "-dev" version) is relevant or not here.

My Question : I'm stuck here. Is there anything else I should do to the yaml or the cabal files, or elsewhere, so that stack build finds the C library?

解决方案

extra-libraries should have just the base library name, so use extra-libraries: MyLib.

更多推荐

Haskell Stack和C库

本文发布于:2023-11-07 00:31:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Haskell   Stack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!