commercialhaskell / commercialhaskell/stack

Linking error when dependency uses own 'extra-library'

Open
#4,141 21 comments 0 reactions 0 assignees View on GitHub
further investigation required type: bug
Dominant language
Haskell
Stars
4.1k
Forks
850
Avg merge
10h 37m
Merged PRs (30d)
4

Description

### General summary/comments (optional)
I get a linking error by GNU gold when trying to add an external library:
``` bash
/usr/bin/x86_64-linux-gnu-ld.gold: error: cannot find -lCacBDD
collect2: error: ld returned 1 exit status
```
I tried it in my regular Ubuntu 18.04 machine and when that failed i used a clean virtual machine installation of the same distribution, I get the same error on both.

### Steps to reproduce
With stack installed, I created a new project with a template:
``` bash
$ stack new my_project_name new-template
```
Then import the module I want in ```src/Lib.hs ``` (which is [HasCacBDD](https://github.com/m4lvin/HasCacBDD)):
``` haskell
module Lib
( someFunc
) where

import Data.HasCacBDD
someFunc :: IO ()
someFunc = putStrLn "someFunc"
```
Then I include the dependency in the ```package.yaml``` file:
```yaml
dependencies:
- base >= 4.7 && < 5
- HasCacBDD
```
And since the package is not included by default in any release of stack, I add the extra dependency in
the ```stack.yaml``` file:
```yaml
extra-deps:
- HasCacBDD-0.1.0.0
```
When I try to build the project, it simply does not work.
### Expected
From a beginners point of view, this configuration looks very similar to the ```acme-missiles``` example
in the [docs](https://docs.haskellstack.org/en/stable/GUIDE/#adding-dependencies), nevertheless, that example works perfectly and this one does not.

I would expect it to build ok or at least show me some infomative message that would help me solve the problem.
### Actual

A linker error.
``` bash
$ stack build
HasCacBDD-0.1.0.0: configureHasCacBDD-0.1.0.0: buildHasCacBDD-0.1.0.0: copy/register
Building all executables for `haskell-smv' once. After a successful build of all of them, only specified executables will be rebuilt.
haskell-smv-0.1.0.0: configure (lib + exe)
Configuring haskell-smv-0.1.0.0...
haskell-smv-0.1.0.0: build (lib + exe)Preprocessing library for haskell-smv-0.1.0.0..Building library for haskell-smv-0.1.0.0..
[1 of 2] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Lib.o )
[2 of 2] Compiling Paths_haskell_smv ( .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/autogen/Paths_haskell_smv.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Paths_haskell_smv.o )
/usr/bin/x86_64-linux-gnu-ld.gold: error: cannot find -lCacBDD
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)
Completed 2 action(s).

-- While building custom Setup.hs for package haskell-smv-0.1.0.0 using:
/home/javier/.stack/setup-exe-cache/x86_64-linux/Cabal-simple_mPHDZzAJ_2.2.0.1_ghc-8.4.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-2.2.0.1 build lib:haskell-smv exe:haskell-smv-exe --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
Process exited with code: ExitFailure 1
```
As I mentioned before, the ```acme-missiles``` example works fine, and also, I am also able to build the package i'm trying to add directly (as explained in the [docs](https://github.com/m4lvin/HasCacBDD)).
### Stack version
I tried with both the latests LTS:
``` bash
$ stack --version
Version 1.7.1, Git revision 681c800873816c022739ca7ed14755e85a579565 (5807 commits) x86_64 hpack-0.28.2
```
and also with the latest nightly ``` --resolver nightly-2018-07-09``` getting the same error on both.

### Method of installation
* Official binary, downloaded from stackage.org or fpcomplete's package repository

### Additional (maybe useful)
[HasCacBDD](https://github.com/m4lvin/HasCacBDD) is a binding to a C package using the FFI. Looking
at the error, it looks like the missing file is a library from that C package. Still, the error seems odd because if you execute the conventional ```stack unpack HasCacBDD-0.1.0.0``` and build it, all the code works just fine. Maybe the issue could be further specified by the question:

> Why is it the case that adding the external package as a dependency does not work the same way as when ```stack unpack``` is used?

Note: Looking at the code, the ```Setup.hs``` of the original package seems to be building and copying the mentioned ```libCacBDD.a``` static library into the correct folder inside the ```.stack-work``` folder (it does so by running some ```Makefile```'s on the original C code and then moving the built library into the folder).

When the package is included as an ```extra-dep```, this ```libCacBDD.a``` library is indeed being stored at ```.stack-work/install/x86_64-linux/resolver_version/ghc_version/lib/x86_64-linux-ghc_version/included_package_name/library.a``` of my project, so probably the only thing that I need to do is to add this folder to library dependencies. It seems awkard and error prone to write the path I mentioned above, since it dependes on the particular resolver, operating system, and other settings of my project. Is there a more robust way to add this folder to my dependencies, maybe in an automatic way?
Thanks in advance!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.