1

I am trying out c2hs, and wanted to compile a small example of a shared library with Cabal to get started.

I have the following relevant section of the cabal file test.cabal:

executable libtest.so
  hs-source-dirs:      src
  main-is:             Dummy.hs
  other-extensions:    ForeignFunctionInterface
  build-depends:       base
  default-language:    Haskell2010
  ghc-options:         -no-hs-main -threaded
  build-tools:         c2hs

Then the source. src/Dummy.hs:

import Test

main :: IO ()
main = return

In the file src/Test.chs

{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign
import Foreign.C

module Android where

And then when I try to compile (cabal configure && cabal build -v) I get the following error message:

Component build order: executable 'libtest.so'
creating dist/build
creating dist/build/autogen
Building test-0.1.0.0...
Preprocessing executable 'libtest.so' for test-0.1.0.0...
Building executable libtest.so...
creating dist/build/libtest.so
creating dist/build/libtest.so/libtest.so-tmp
/opt/ghc/bin/ghc --make -no-link -fbuilding-cabal-package -O -j8 -static
-outputdir dist/build/libtest.so/libtest.so-tmp -odir dist/build
/libtest.so/libtest.so-tmp -hidir dist/build/libtest.so/libtest.so-tmp-stubdir
dist/build/libtest.so/libtest.so-tmp -i -idist/build/libtest.so/libtest.so-tmp
-isrc -idist/build/autogen -Idist/build/autogen
-Idist/build/libtest.so/libtest.so-tmp
-optP-include -optPdist/build/autogen/cabal_macros.h -hide-all-packages
-package-db dist/package.conf.inplace -package-id
base-4.7.0.1-1a55ebc8256b39ccbff004d48b3eb834 -XHaskell2010
src/Dummy.hs -no-hs-main -threaded

src/Dummy.hs:1:8:
    Could not find module ‘Test’
Use -v to see a list of the files searched for.

Please, can you tell me what the cause of the error is? What am I missing?

rhaps0dy
  • 1,236
  • 11
  • 13

1 Answers1

2

You need to add Test to the other-modules field.

Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77
  • But isn't that for exporting the module as if it was a library? – rhaps0dy Jun 25 '15 at 06:22
  • 1
    No, it's for telling Cabal about modules which it's not necessarily able to figure out for itself (which can happen for many valid reasons... not worth diving into here). In this case, Cabal needs to know that it needs to generate the .hs file for GHC. – Michael Snoyman Jun 25 '15 at 09:50