Home > Software engineering >  Cabal how to install and link to C source code
Cabal how to install and link to C source code

Time:11-09

I'm using inline-c and I have two C files that I'd like to call in the Haskell code. In the Cabal file, there are:

include-dirs:     cbits
                , /usr/local/include
c-sources:        cbits/genann.c 

In the Cbits directory, there's also genann.h

I can build and run the project without problem but when I do cabal install, the compiler complain that it can't find the C header file.

I checked the Cabal manual and added the following options:

includes:         cbits/genann.h
install-includes: cbits/genann.h

This time I got:

Configuring executable 'inline-gsl' for inline-gsl-0.1.0.0..
cabal-3.6.2.0: Missing dependency on a foreign library:
* Missing (or bad) header file: cbits/genann.h
This problem can usually be solved by installing the system package that
provides this library (you may need the "-dev" version). If the library is

Where is wrong here? Do I need to compile the C code into object code and put them in the system?

The complete Cabal file:

cabal-version:      2.4
name:               inline-gsl
version:            0.1.0.0

-- A short (one-line) description of the package.
-- synopsis:

extra-source-files: CHANGELOG.md

executable inline-gsl
    main-is:          Main.hs


    build-depends:    base ^>=4.14.3.0
                    , inline-c
                    , vector
                    , massiv
                    , array
                    , bytestring >= 0.10
                    , template-haskell
    hs-source-dirs:   app
    pkgconfig-depends: glib-2.0, gtk4, gsl
    default-language: Haskell2010
    extra-libraries:  m, tensorflow
    extra-lib-dirs:   /usr/local/lib
    cc-options:       -Wall -m64 -O4
    include-dirs:     cbits
                    , /usr/local/include
    includes:         cbits/genann.h
    install-includes: cbits/genann.h
    c-sources:        cbits/genann.c 

CodePudding user response:

I can build and run the project without problem but when I do cabal install, the compiler complain that it can't find the C header file.

I believe this indicates that the problem is that your header files are not included in your package source tarballs (sdist). You need to list all non-Haskell source files under the extra-source-files field in your cabal file:

extra-source-files: CHANGELOG.md, cbits/genann.h, cbits/genann.c
  • Related