[Haskell-beginners] Linking C++ in Cabal

Leonhard Applis Leonhard.Applis at protonmail.com
Sun May 24 16:30:22 UTC 2020


Hello,

I want to use a foreign C++ Function in a Haskell Library, and the Library in a Haskell Program.
The C++ function is separate in a file and I have a header file. 
I made a minimal reproducing repository.

The closes I have found is this repository which wraps the c++ in c and makes a full library, needing a makefile etc.
So while I think I'd get it working like that, it looks to be quite a lot to do for my simple task.

the repository structure looks as follows:

Projekt

-   cpp-src

-   some.cpp
-   some.h

-   lib-src

-   CPPLib.hs

-   program-src

-   program.hs

-   project.cabal



And my (currently failing) project.cabal the following:

cabal-version:  3.0
name: cpp_lib
[...]
library
exposed-modules: CPPLib
other-extensions: ForeignFunctionInterface
build-depends: base
hs-source-dirs: lib-src
default-language: Haskell2010
include-dirs: ./cpp-src
cxx-sources: ./cpp-src/some.cpp
install-includes: ./cpp-src/some.h

executable CPPApp
main-is: program.hs
default-language: Haskell2010
build-depends: HaskellForeignCPP,
base
hs-source-dirs: program-src

The some.cpp is trivial for this case lets assume its a function multiply :: Int -> Int -> Int, and both .cpp and .h are well-formed and don't have further dependencies.

The library compiles fine, but when building the executable it fails telling me

collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)

Can someone help me out?
I find very little documentation on this topic.

I know there is Inline-CPP which would help as well, but I just very much like the idea of having a nice separate file and interface for modularity.

best
Leonhard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 477 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment.sig>


More information about the Beginners mailing list