[Hackage] #504: executables with a C main function

Hackage trac at galois.com
Sun Feb 22 12:09:13 EST 2009


#504: executables with a C main function
------------------------------+---------------------------------------------
  Reporter:  duncan           |        Owner:         
      Type:  enhancement      |       Status:  new    
  Priority:  normal           |    Milestone:         
 Component:  Cabal library    |      Version:  1.6.0.1
  Severity:  normal           |     Keywords:         
Difficulty:  easy (<4 hours)  |   Ghcversion:         
  Platform:                   |  
------------------------------+---------------------------------------------
 We would like to be able to do:

 {{{
 executable blah
   main-is: main.c
 }}}

 Either for pure C programs or mixed C/Haskell programs with main in the C
 code rather than Haskell code.

 == Workaround ==

 Until this is implemented one can use a main in Haskell that calls out to
 the C main function. For example:

 `main.c`
 {{{
 int real_main () {
   return 42;
 }
 }}}

 `Main.hs`
 {{{
 import System.Exit
 import Foreign.C
 main = real_main >>= exitWith . ExitFailure . fromIntegral
 foreign import ccall "real_main" real_main :: IO CInt
 }}}

 `c-main.cabal`
 {{{
 name: c-main
 version: 0.0
 build-type: Simple
 cabal-version: >= 1.2
 extra-source-files: main.h

 executable c-main
   main-is: Main.hs
   c-sources: main.c
   includes: main.h
   extensions: ForeignFunctionInterface
   build-depends: base
 }}}

 == Implementation notes ==

 At the moment Cabal's "Simple" build system requires `main-is` to specify
 a `.hs` or `.lhs` file (though it does allow that to be generated by a
 pre-processor). If we lift that restriction the first thing to go wrong
 will be that `ghc --make` does not work with `-no-hs-main`. We will have
 to do the build and link steps separately (which is probably a good thing
 anyway). We would use `ghc --make` to compile all Haskell modules to `.o`
 files and then invoke `ghc` in batch mode passing it all the `.o` files
 (from Haskell and C modules) and `-package` flags. If we're using a C main
 then we'd do the link step with `-no-hs-main`.

-- 
Ticket URL: <http://hackage.haskell.org/trac/hackage/ticket/504>
Hackage <http://haskell.org/cabal/>
Hackage: Cabal and related projects


More information about the cabal-devel mailing list