这可能是这篇文章的重复.但它得到的唯一答案对我来说似乎不起作用,所以我在这里发布自己的案例,希望找到一个具体的解决方案.我在Linux Ubuntu Trusty上.
我有一个专有的C库libMyLib.so,位于/ usr/local/lib(包含在LD_LIBRARY_PATH中).
我用uskell的方式写了这个C库的haskell绑定.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 -- ...
我可以用ghci来测试这个程序,将它传递给库和它可以找到它的目录:ghci /usr/local/lib/ -lMyLib
它工作得很好.
现在,独立地,我正在尝试开始使用堆栈,并按照本指南,我能够构建几个非平凡的程序示例.
但是,我无法堆叠构建上面的FFI Main.hs程序,因为它抱怨丢失的C库:
第一次尝试 :
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
在第二次尝试之前,我确实添加了以下内容:
- 添加extra-lib-dirs: [/usr/local/lib]
到stack.yaml文件中.
- 添加extra-include-dirs: [/usr/local/lib]
到stack.yaml文件中(尽管可能不需要).
- extra-libraries: lMyLib
在标签可执行文件或库下添加到项目的cabal文件中(我也试过extra-libraries: libMyLib.so
了相同的结果).
第二次尝试:
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
所以现在它知道它需要哪个C库,因为它抱怨它,同时添加了正确的额外库目录(参见错误消息的最后).我不确定评论(you may need the "-dev" version)
是否相关.
我的问题:我被困在这里.我还应该对yaml或cabal文件或其他地方做任何其他事情,以便stack build
找到C库吗?
extra-libraries
应该只有基本库名称,所以使用extra-libraries: MyLib
.