Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Producing multiple executables from single project

Tags:

With the following project structure:

src/FirstExecutable.hs src/SecondExecutable.hs my-amazing-project.cabal 

and the following cabal setup:

name:               my-amazing-project version:            0.1.0.0 build-type:         Simple cabal-version:      >=1.8  executable first-executable   hs-source-dirs:   src   main-is:          FirstExecutable.hs   ghc-options:      -O2 -threaded -with-rtsopts=-N   build-depends:    base == 4.5.*  executable second-executable   hs-source-dirs:   src   main-is:          SecondExecutable.hs   ghc-options:      -O2 -threaded -with-rtsopts=-N   build-depends:    base == 4.5.* 

Running cabal install fails with the following output:

Installing executable(s) in /Users/mojojojo/Library/Haskell/ghc-7.4.2/lib/my-amazing-project-0.1.0.0/bin cabal: dist/build/second-executable/second-executable: does not exist Failed to install my-amazing-project-0.1.0.0 cabal: Error: some packages failed to install: my-amazing-project-0.1.0.0 failed during the final install step. The exception was: ExitFailure 1 

What am I doing wrong or is this a Cabal bug?


The contents of the executable modules are as follows:

module FirstExecutable where  main = putStrLn "Running FirstExecutable" 

and

module SecondExecutable where  main = putStrLn "Running SecondExecutable" 
like image 438
Nikita Volkov Avatar asked Jan 09 '13 15:01

Nikita Volkov


People also ask

What is the common way to have several executables in your project?

You can build different EXE files based on the same project using compiler defines. You can even define your own defines. But per compile-run you can only build one binary (DLL, EXE) per project - one or the other, but not both. Save this answer.

How do I create an executable Visual Studio project?

To build your program and create teh executable file choose Build My Project.exe from the Build menu - "My Project" represents teh name you chose for your project and the extension ".exe" is used to designate that the file being created will be an executable file.


1 Answers

cabal expects the module of the executable to be Main. You should skip the module line or use module Main where.

Ok here is the possible reason. The executable of a haskell program is not produced when the module is not Main when you actually compile the program. The main function of the Main module is used when the executable is run. A possible workaround for ghc is -main-is flag. So you can have something like

name:               my-amazing-project version:            0.1.0.0 build-type:         Simple cabal-version:      >=1.8  executable first-executable   hs-source-dirs:   src   main-is:          FirstExecutable.hs   ghc-options:      -O2 -threaded -with-rtsopts=-N -main-is FirstExecutable   build-depends:    base == 4.5.*  executable second-executable   hs-source-dirs:   src   main-is:          SecondExecutable.hs   ghc-options:      -O2 -threaded -with-rtsopts=-N -main-is SecondExecutable   build-depends:    base == 4.5.* 
like image 171
Satvik Avatar answered Dec 02 '22 21:12

Satvik