How do I build basic GHC packages with -fPIC?

Viacheslav Lotsmanov lotsmanov89 at
Tue Dec 26 20:11:46 UTC 2017

Hi there. My actual main goal is to build my own shared library written 
in Haskell that would be compatible with application written in C even 
without knowing that is is written in Haskell. So for now I compiled my 
shared library but I only could dynamically link it to Haskell 
dependencies such as "base" and "ghc-prim" packages. But I want to 
statically link Haskell dependencies but I realized it isn't simple and 
straightforward task.

On Freenode's #haskell I was advised I should build GHC from scratch 
with -fPIC, on the Linux (I'm using Fedora Workstation 25 on x86_64) I 
couldn't go forward without this step. So I wrote some Dockerfile based 
on Debian 9, skipping first part which is containing 'apt-get update' 
and installing 'build-essential' here is what I have:


RUN mkdir /compile && cd /compile \
     && wget \
     && tar -xvf ghc-8.2.2-src.tar.xz \
     && rm ghc-8.2.2-src.tar.xz \
     && cd ghc-8.2.2/ \
     && ./configure --prefix=/ghc-8.2.2-fpic --disable-library-profiling 
--enable-shared \
     && cp / mk/ \
     && make install \
     && cd /usr/local/bin \
     && ls /ghc-8.2.2-fpic/bin/ | xargs -I{} ln -s /ghc-8.2.2-fpic/bin/{}

And as you can see I just use my own prepared / file which is:

SRC_HC_OPTS          = -H64m -O
EXTRA_HC_OPTS        = -fPIC
SRC_CC_OPTS          = -fPIC -O
GhcStage1HcOpts      = -fasm -O0
GhcStage2HcOpts      = -fasm -O0
GhcLibHcOpts         = -fasm -O2
GhcLibWays           = v dyn
SplitObjs            = NO
V                    = 1
LATEX_DOCS           = NO
BeConservative       = YES

I just combined it from parts I found in the internet during searching 
answers to my questions. So I built this container, I also installed 
dependencies by this commands:

cd /mnt
cabal update
cabal sandbox init
cabal install --enable-shared --ghc-option=-fPIC happy alex
cabal install --enable-shared --ghc-option=-fPIC base-unicode-symbols 
filepath process directory lens containers qm-interpolated-string

And when I tried to build my app by following commands (first command 
compiles some C-code to automatically initialize Haskell runtime, see 
link posted below, not sure if /-static/, /-shared/ or /-fPIC/ means 
something here but it's work in progress):

ghc -static -shared -fPIC -optc-DMODULE=Foo src/lib-autoinit.c 
-outputdir builddir
ghc -package-db=SOME_CABALS_SANDBOX_PKGDB_DIR --make-static-shared-fPIC 
src/Foo.hs builddir/src/lib-autoinit.o -o builddir/libfoo.o -isrc 
-outputdir builddir -Wall -O2

I failed with a lot of similar errors like this one:

/usr/bin/ error: 
requires dynamic R_X86_64_PC32 reloc against 'stg_ap_0_fast' which may 
overflow at runtime; recompile with -fPIC

What have I missed? What should I do to make this happen?

Any progress could be found here (Dockerfile, sources of modules, 
Related stack overflow issue:

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the ghc-devs mailing list