使用Brew安装Haskell Stack时遭遇iconv链接错误求助
Hey, I've run into this exact issue before on macOS Mojave when setting up Stack for Haskell—those iconv linker errors are a real pain, but we can fix them with a few tweaks. Let's break down what's going on and how to resolve it:
Why This Happens
The error boils down to the linker not being able to find the iconv symbols your Haskell setup needs, even though you have libiconv installed on your system. Stack's sanity check compilation step isn't automatically linking against the system's libiconv, and your pyenv shims for iconv might be adding a layer of confusion (though that's probably not the root cause here).
Fixes to Try
1. Explicitly Link Libiconv When Running stack setup
The quickest fix is to pass the necessary linker flags directly when running stack setup. Open your terminal and run:
LDFLAGS="-L/usr/lib -liconv" CPPFLAGS="-I/usr/include" stack setup
This tells the compiler where to find the libiconv headers and library, which should resolve the undefined symbols error.
If you want these flags to persist for future Stack commands, add them to your shell profile (like ~/.bash_profile or ~/.zshrc):
export LDFLAGS="-L/usr/lib -liconv" export CPPFLAGS="-I/usr/include"
Restart your terminal or run source ~/.zshrc (or your profile file) to apply changes, then run stack setup again.
2. Adjust Your PATH to Prioritize System Iconv (Optional)
Since your which iconv points to pyenv's shims, you can temporarily or permanently move the system's iconv to the front of your PATH to ensure Stack uses the system version:
export PATH="/usr/bin:$PATH"
This is probably not the main issue, but it's worth trying if the first fix doesn't work.
3. Configure Stack to Always Use These Flags
For a more permanent solution, edit your Stack config file (~/.stack/config.yaml) and add these lines:
extra-include-dirs: [/usr/include] extra-lib-dirs: [/usr/lib] extra-link-opts: [-liconv]
This ensures every time Stack compiles code, it will include these paths and link against libiconv automatically.
4. Reinstall Stack via Homebrew
If none of the above work, try reinstalling Stack to rule out a corrupted installation:
brew reinstall stack
Then repeat the first fix with the linker flags.
After trying any of these, rerun stack setup—it should complete without those linker errors.
内容的提问来源于stack exchange,提问作者Masanari Makino

