commercialhaskell / stack

The Haskell Tool Stack
http://haskellstack.org
BSD 3-Clause "New" or "Revised" License
3.99k stars 845 forks source link

[Windows] extra-libraries and extra-lib-dirs are not passed to the linker / ghc for linking when building a haskell DLL that depends on another foreign DLL #4135

Open arvindd opened 6 years ago

arvindd commented 6 years ago

bugexample.zip

General summary

This bug surfaces in the following scenario:

Although I have extra-lib-dirs in my stack.yaml and extra-libraries package.yaml, I see these do not get passed on to the linker while building the Haskell DLL.

I had to specifically add -L (for searching library directories) and -l (for linking a specific .lib file) in my ghc-options (apart from also having extra-lib-dirs and extra-libraries.

Steps to reproduce

  1. Unzip the example stack project (attached, zip file) to this directory: c:\workspace\haskell\bugexample
  2. Build the example (how to build is in the Readme.md file within the above zip)

Expected

When I already have put in extra-lib-dirs in my stack.yaml and extra-libraries package.yaml, it should be possible that HSDll.dll is getting built without any problems.

Actual

Although I have used those options, ghc is unable to find the foreign library, and hence building of HSDll.dll fails with "missing library"

Workaround

As a workaround, I put these lines as additional lines in the ghc-options in package.yaml:

     - -lclib
     - -LC:\\workspace\\Haskell\\bugexample\\src\\clib

With this, build succeeds, although I get a warning from stack that says that extra-lib-dirs should be used instead of -L and extra-libraries must be used instead of -l options.

Partial output of the build

PS C:\workspace\Haskell\bugexample> stack build
Building all executables for `bugexample' once. After a successful build of all of them, only specified executables will be rebuilt.
bugexample-0.0.0: configure (lib + exe)
Configuring bugexample-0.0.0...
Cabal-simple_Z6RU0evB_2.0.1.0_ghc-8.2.2.exe: Missing dependency on a foreign
library:
* Missing C library: clib
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.

Stack version

S C:\workspace\Haskell\bugexample> stack --version
Version 1.7.1, Git revision 681c800873816c022739ca7ed14755e85a579565 (5807 commits) x86_64 hpack-0.28.2

Method of installation

Official binary, downloaded from stackage.org or fpcomplete's package repository

mihaimaruseac commented 6 years ago

I don't have access to a Windows, so I cannot attempt a reproduction. Waiting for someone else to reproduce.

arvindd commented 6 years ago

How can I help you here? Do you need some logs from the build of the bugexample.zip that I've attached? Is there some specific thing that I can do before running the build (eg: env variables, etc.) so that you get more information for debugging?

exodrifter commented 4 years ago

I'm not sure what is needed for a reproduction, but I decided to give it a shot since I have access to a Windows machine and I've encountered similar linking problems (unrelated to this specific issue) while trying to figure out to build Win32 DLLs.

I was able to reproduce the issue by following the instructions in the README.md file. Here's the non-verbose output of the failed build (including the versions of windows, gcc, and stack):

PS C:\workspace\Haskell\bugexample> systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.18362 N/A Build 18362

PS C:\workspace\Haskell\bugexample> gcc --version
realgcc.exe (Rev1, Built by MSYS2 project) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

PS C:\workspace\Haskell\bugexample> stack --version
Version 2.3.3, Git revision cb44d51bed48b723a5deb08c3348c0b3ccfc437e x86_64 hpack-0.33.0

PS C:\workspace\Haskell\bugexample> stack build
bugexample> configure (lib + exe)
Configuring bugexample-0.0.0...
bugexample> build (lib + exe)
Preprocessing library for bugexample-0.0.0..
Building library for bugexample-0.0.0..
Linking main.exe ...
.stack-work\dist\5c8418a7\build\HLib.o:fake:(.text+0x12a): undefined reference to `printMsg'
collect2.exe: error: ld returned 1 exit status
`gcc.exe' failed in phase `Linker'. (Exit code: 1)

--  While building package bugexample-0.0.0 using:
      C:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_2.0.1.0_ghc-8.2.2.exe --builddir=.stack-work\dist\5c8418a7 build lib:bugexample exe:bugexample-exe --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

As described in the original post, the build succeeds with the described workaround.

I've also attached the verbose output of the failed build here: build-verbose-output.txt

bapcyk commented 3 years ago

I have the same problem but in Linux. And ghc-options with -L... helps too. So, IMHO it's not Windows specific.