Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

compilation - Haskell dynamic library

http://www.vex.net/~trebla/haskell/so.xhtml describes how to compile shared library.

About compiling command:

ghc -O2 -dynamic -shared -fPIC -o libEval.so Eval.hs hsbracket.c -lHSrts-ghc7.6.3

it says:

(Could you omit -dynamic to request static libraries of other packages? Not really, they were not generated with -fPIC. In particular it is illegal on x86_64.)

Why is it so? What should one do to compile shared library without libHS* dependencies?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since Kaiko contacted me privately for an answer, might as well post it here...

Short version

By omitting -dynamic you would be trying to take all the static .a libs and link them into one massive .so file. Those .a libs were themselves built without -fPIC. All code that ends up on a .so file must be built with -fPIC (at least on ELF x86-64). Thus the linking would fail in this case because -fPIC is required but the libs were not built with -fPIC.

Long version

There are a few things that vary between different ways of building static and dynamic libraries:

  1. Is it built as a .a archive or as a .so (or .dll/.dynlib) object?
  2. Is it built with -fPIC, position independent code or not?
  3. Are external symbols expected to be in the same DSO or an external DSO?

In principle, lots of different combinations of these things make sense but in practice only a few are used.

On Linux (ELF), there are two standard approaches to building libraries, fully static and fully dynamic. In the fully static approach the answer to question 1,2,3 above are: .a archive, no -fPIC, same DSO. In the fully dynamic approach the answers are: .so lib, -fPIC, external DSO.

Now what you want to do is different. You want all libraries to be built as .a files, but with -fPIC and external symbols expected to be in the same DSO. This would then let you link all those libraries together into one huge shared library. So the crucial difference is the use of -fPIC, since on ELF (specifically x86_64) code that ends up in a shared lib must be built with -fPIC.

By contrast, on Windows, GHC can do exactly what you want, to link all the Haskell libs (including the RTS etc) into one massive shared lib (.dll). This is because on Windows (unlike ELF), position independent code does not matter. So on Windows, one is able to take the static libraries and link them into a big shared library.

In principle this should also be possible on Linux, if all of the Haskell libraries were built statically but with -fPIC. This is not the default, and that is the immediate reason why you cannot omit -dynamic in this case on Linux.

So in principle, one could try rebuilding ghc and the core libraries from source using the -fPIC flag and then see if it then works to omit -dynamic and link everything into one huge shared lib.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...