Haskell - Cabal linker command failed when installing Text.Regex.Posix

53 views Asked by At

I'm pretty new to working with Haskell. My professor is having us use it for class.

Currently working on Mac. While going through some tutorials, I found out I had to download the regex package for some of them to work. I've tried downloading the package through cabal, but I keep getting an error when it tries to build:

clang: error: linker command failed with exit code 1

I haven't been able to find any working solutions to this problem, as the ones that I have found have relied on installing the Haskell platform, which seems to be practically defunct as of last year (the page linking to it literally tells me to go away and use GHC).

I did attempt to redownload cabal following these instructions, but when I try to install regex I get the same error.

I am very much at a loss.

1

There are 1 answers

0
Jon Purdy On

Welcome!

Either Homebrew or ghcup should be fine for your purposes. By “downloading the package through cabal” I infer that you tried cabal install regex. Unfortunately, this likely isn’t what you need—it’s meant for using Cabal to build and install a program, not for installing a library for a program to use.

You can run a single-file program by specifying its dependencies in a header at the top of the file, and using cabal run.

#!/usr/bin/env cabal
{- cabal:
build-depends: base, regex
-}

import Text.RE

main :: IO ()
main = putStrLn "example"

If this is saved in Example.hs, then you should be able to run it with cabal run Example.hs.

The more common workflow for using a package is to make a project with cabal init.

cabal init example

Then you can add a dependency in the build-depends section of the Cabal file, in this case example.cabal. Here you can write regex by itself or include a version constraint such as regex >= 1.1.0.2 (the latest as I write this). Then you should be able to build your project with cabal build and run it with cabal exec.