Static .lib required for compiling library is also required for library users?
Asked Answered
S

1

7

I've been working on a Haskell library package which requires a custom .dll and .lib on Windows to talk to some OS APIs. The .lib is linked into the library with the extra-libraries field and the DLL is installed in the cabal package directory with data-files.

For some reason (I'm not an expert on linking by any means, but this seems weird) if I create a test executable which uses my package (in the build-depends field), it wants to link in the same .lib used to compile the library - even though it's just calling library functions, not anything the .lib exposes. Obviously it needs access to the .dll at runtime but that is to be expected. Needing the .lib as well seems odd.

I would expect that the .lib would already be linked into the .a file generated by Cabal/GHC for my library when it's installed. Is this not the case? And if it is, can someone explain why it's that way?

Salivate answered 2/7, 2013 at 22:56 Comment(0)
I
1

Looks like you want partial linking (see --relocatable flag in ld manual page). As I can see from the sources, cabal uses partial linking only libraries, compiled for ghci. From Distribution.Simple.GHC (buildLib function):

whenVanillaLib False $ do
  (arProg, _) <- requireProgram verbosity arProgram (withPrograms lbi)
  Ar.createArLibArchive verbosity arProg
    vanillaLibFilePath staticObjectFiles

whenProfLib $ do
  (arProg, _) <- requireProgram verbosity arProgram (withPrograms lbi)
  Ar.createArLibArchive verbosity arProg
    profileLibFilePath profObjectFiles

whenGHCiLib $ do
  (ldProg, _) <- requireProgram verbosity ldProgram (withPrograms lbi)
  Ld.combineObjectFiles verbosity ldProg
    ghciLibFilePath ghciObjFiles

whenSharedLib False $
  runGhcProg ghcSharedLinkArgs

You can see, that for vanilla and profiling libraries, cabal just calls ar utility (see createArLibArchive). For ghci it calls ld with -r flag (which is a shortcut for --relocatable) (see combineObjectFiles).

So, cabal doesn't actually do any linking for vanilla libraries, it just combines object files. Actually cabal can't know, whether final application will use any symbol from your extra-lib, so the behavior seems reasonable.

Ir answered 3/7, 2013 at 11:26 Comment(2)
Is there a way to force it to use ld -r instead of ar for vanilla libraries? I've tried adding ld-options: -r but it doesn't seem to do anything (presumably because ld isn't being used?)Salivate
@TomSavage No, I think it is not possible. But I'm not an expert in cabal.Ir

© 2022 - 2024 — McMap. All rights reserved.