[SOLVED] Build Trouble: zlib.h 0.7.1.0

Hi!

I’m having trouble building one of my cabal projects, receiving this build error from cabal:

Failed to build zlib-0.7.1.0. The failure occurred during the configure step.
Build log (
/home/ola/.cache/cabal/logs/ghc-9.4.8/zlib-0.7.1.0-ea2ccc2f93d951d84a88c78a7b4f196fea6a3780cdafe6f13025c03d6aaa9dce.log
):
Configuring library for zlib-0.7.1.0..
Error: .cabal-wrapped: Missing dependency on a foreign library:
* Missing (or bad) header file: zlib.h
* Missing (or bad) C library: z
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...

I have tried installing zlib and variations, but that hasn’t worked. I figured something could have changed, and sure enough in April this happened:

0.7.1.0 Bodigrim andrew.lelechenko@gmail.com April 2024

    Split zlib C sources into zlib-clib package (thanks @hasufell).
    Use zlib-clib on Windows, unless pkg-config is available.

So something that might be worth testing is to use an older version, but since the project is only indirectly depending on zlib, how do I pinpoint an earlier version in my .cabal?

It could be worth mentioning that I switched to NixOS some time ago.

The project on github

2 Likes

You can enter a new nix shell with pkg-config and zlib:

nix-shell -p pkg-config zlib

Inside that shell cabal should be able to see the pkg-config dependencies.

I’m not sure if that is the most idiomatic way.

1 Like

Yeah, that was the first thing I reached for. Unfortunately it results in the same build error.

Edit: I’m sure I tried with pkg-config, but I’ll have to test again when this uni lecture ends

Edit 2: Yeah, I had tried that. No luck!

1 Like

Looks like somebody had a similar issue on StackOverflow, and including the package in your environment isn’t enough: haskell - How do I supply a C library to stack on NixOS? - Stack Overflow

1 Like

You can create cabal.project and put

constraints:
  zlib +bundled-c-zlib

I’m not sure what’s up with your environment. While there were changes to Windows setup in the recent zlib release, Unix builds should have been unaffected.

1 Like

Your error indeed seems like the one I get if I forget zlib entirely. But I sometimes receive an error if I do not add zlib to LD_LIBRARY_PATH, so it may be worth trying to preempt that. A minimal shell might look like:

let
  ldDeps = [ pkgs.zlib ... ];
  deps = [ e.g. cabal-install, ghc ] ++ ldDeps;
in
{
  devShells.x86_64-linux = {
    default = pkgs.mkShell {
      buildInputs = deps;
      LD_LIBRARY_PATH = "${pkgs.lib.makeLibraryPath ldDeps}:$LD_LIBRARY_PATH";
    };
  };
}
1 Like

I was unable to build your package because you have a base constraint ^>=4.17.2.1, but you also have a dependency on servant-event-stream, for which no version supports base >4.15. :slight_smile: How do you build this project normally?

I’m currently reading and trying out the possible solutions above, but I normally just run cabal build --allow-newer=all, as some dependencies won’t build otherwise because of version mismatch. Can not remember which packages exactly, but some are more troublesome than others :slight_smile:

This worked! For future reference, the .project file ended up looking like this:

packages: .

constraints:
  zlib +bundled-c-zlib

as packages seems to be a mandatory field.

I would probably have had a hard time figuring this out myself - thank you!

Edit: I’m also going to test tbidne’s solution, as my environment probably is at fault.

2 Likes

I had this issue trying to cabal install turtle (or it might have been hpack) on a docker image:


# Update the package list and install some basic utilities
RUN apt-get update && \
    apt-get install -y \
    wget \
    git \
    sudo \
    # GHCUp Requirements https://www.haskell.org/ghcup/install/#version-23
    build-essential \
    curl \
    build-essential \ 
    libffi-dev \
    libffi8ubuntu1 \
    libgmp-dev \
    libgmp10 \
    libncurses-dev \
    pkg-config \
    # used by zlib used by turtle
    && apt-get clean \
    # get ghcup install script
    && curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | BOOTSTRAP_HASKELL_NONINTERACTIVE=1 \
                                                                            BOOTSTRAP_HASKELL_GHC_VERSION=9.8.2 \
                                                                            BOOTSTRAP_HASKELL_CABAL_VERSION=3.12.1.0 \
                                                                            BOOTSTRAP_HASKELL_INSTALL_NO_STACK=1 \
                                                                            BOOTSTRAP_HASKELL_INSTALL_HLS=1 \
                                                                            sh

    # Explicitly export PATH changes
    ENV PATH="/root/.ghcup/bin:${PATH}"

    # install other tools
    RUN cabal update 

    RUN cabal install ghcid

    RUN cabal install hpack

    RUN cabal install turtle

    RUN apt-get install -y bash

Solution turned out to be adding: zlib1g-dev

...

    RUN apt-get install -y zlib1g-dev

    RUN cabal install hpack

    RUN cabal install turtle
...
1 Like