-->

Haskell Stack and C Libraries

2020-08-13 04:39发布

问题:

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?

回答1:

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