[Haskell-beginners] Linking C++ in Cabal
鲍凯文
traqueofziche at gmail.com
Tue May 26 20:03:13 UTC 2020
Hi,
So I tried it out, and there're a couple of things:
- Declaring a function as `static` will hide it from anything other than
the current translation unit, AFAIK, which means that you won't be able to
link it to anything else; the symbol won't be visible (you can verify using
`objdump`).
- For C++ functions, you need to suppress name-mangling since Haskell's FFI
doesn't know about that. With your repo as is, the symbol corresponding to
the `multiply` function is not...`multiply`:
```
dist-newstyle/build/x86_64-linux/ghc-8.6.5/HaskellForeignCPP-0.0.1/build/cpp-src/some.o:
file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 some.cpp
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack
0000000000000000 l d .eh_frame 0000000000000000 .eh_frame
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000000 g F .text 0000000000000006 _Z8multiplyii
```
so you just need to wrap every exposed function in `extern "C"` blocks,
like it's explained here: https://www.geeksforgeeks.org/extern-c-in-c/.
With `extern "C"` and removing `static`:
```
dist-newstyle/build/x86_64-linux/ghc-8.6.5/HaskellForeignCPP-0.0.1/build/cpp-src/some.o:
file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 some.cpp
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack
0000000000000000 l d .eh_frame 0000000000000000 .eh_frame
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000000 g F .text 0000000000000006 multiply
```
...and everything builds fine.
Best,
toz
On Tue, May 26, 2020 at 5:03 AM <beginners-request at haskell.org> wrote:
> Send Beginners mailing list submissions to
> beginners at haskell.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> or, via email, send a message with subject or body 'help' to
> beginners-request at haskell.org
>
> You can reach the person managing the list at
> beginners-owner at haskell.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Beginners digest..."
>
>
> Today's Topics:
>
> 1. Re: Linking C++ in Cabal - repository Link (Leonhard Applis)
> 2. Re: Linking C++ in Cabal (鲍凯文)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 25 May 2020 13:00:03 +0000
> From: Leonhard Applis <Leonhard.Applis at protonmail.com>
> To: "beginners at haskell.org" <beginners at haskell.org>
> Subject: Re: [Haskell-beginners] Linking C++ in Cabal - repository
> Link
> Message-ID:
>
> <iF7eLvkM4HniAVDyO09U-O3y48pPEAciKs5zjKAtajj8lldf1g8Tp5UHi3-HiT2GMc15GBi5JgVgkRXr4KQ8Y8p6i0CuuAU-RLh-g2t57uQ=@
> protonmail.com>
>
> Content-Type: text/plain; charset="utf-8"
>
> Ups, in my mailing list the links have disappeared.
> Just in case:
>
>
> The minimal reproducing repository:
>
> https://github.com/Twonki/HaskellForeingCPPMinimal
>
> The repository from Aditya Siram with C and makefile:
>
> https://github.com/deech/CPlusPlusBindings
>
> best
> Leonhard
>
>
>
> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> On Monday, May 25, 2020 2:00 PM, <beginners-request at haskell.org> wrote:
>
> > Send Beginners mailing list submissions to
> > beginners at haskell.org
> >
>
> > To subscribe or unsubscribe via the World Wide Web, visit
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> > or, via email, send a message with subject or body 'help' to
> > beginners-request at haskell.org
> >
>
> > You can reach the person managing the list at
> > beginners-owner at haskell.org
> >
>
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of Beginners digest..."
> >
>
> > Today's Topics:
> >
>
> > 1. Linking C++ in Cabal (Leonhard Applis)
> >
>
> >
>
> > Message: 1
> > Date: Sun, 24 May 2020 16:30:22 +0000
> > From: Leonhard Applis Leonhard.Applis at protonmail.com
> > To: "beginners at haskell.org" beginners at haskell.org
> > Subject: [Haskell-beginners] Linking C++ in Cabal
> > Message-ID:
> >
> 51wOfngyX74dYjJRw6hibqw07vGHvvSYPD_f3f2-_F7hG0k0GwWf2Xv4g7LyeZNyn_vR28s6GIbd3e-78_TapNIEo_tJZ-2fTViOmkQYOnM=@
> protonmail.com
> >
>
> > Content-Type: text/plain; charset="utf-8"
> >
>
> > 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-0001.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-0001.sig
> >
>
> > --
> >
>
> > Subject: Digest Footer
> >
>
> > Beginners mailing list
> > Beginners at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >
>
> >
>
> >
> ---------------------------------------------------------------------------------------------------------
> >
>
> > End of Beginners Digest, Vol 143, Issue 12
>
> -------------- 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/20200525/0e889731/attachment-0001.sig
> >
>
> ------------------------------
>
> Message: 2
> Date: Mon, 25 May 2020 16:45:09 -0700
> From: 鲍凯文 <traqueofziche at gmail.com>
> To: beginners at haskell.org
> Subject: Re: [Haskell-beginners] Linking C++ in Cabal
> Message-ID:
> <CAMjcG+FWPZtkM7soC34VOdSBQ+ArdWY2Bpk2Wm=
> XNqEdg3ox2Q at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Hi,
>
> Would it be possible to upload your full repo somewhere so people could see
> everything and possibly reproduce this?
>
> Thanks,
>
> toz
>
> On Mon, May 25, 2020 at 5:03 AM <beginners-request at haskell.org> wrote:
>
> > Send Beginners mailing list submissions to
> > beginners at haskell.org
> >
> > To subscribe or unsubscribe via the World Wide Web, visit
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> > or, via email, send a message with subject or body 'help' to
> > beginners-request at haskell.org
> >
> > You can reach the person managing the list at
> > beginners-owner at haskell.org
> >
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of Beginners digest..."
> >
> >
> > Today's Topics:
> >
> > 1. Linking C++ in Cabal (Leonhard Applis)
> >
> >
> > ----------------------------------------------------------------------
> >
> > Message: 1
> > Date: Sun, 24 May 2020 16:30:22 +0000
> > From: Leonhard Applis <Leonhard.Applis at protonmail.com>
> > To: "beginners at haskell.org" <beginners at haskell.org>
> > Subject: [Haskell-beginners] Linking C++ in Cabal
> > Message-ID:
> >
> >
> <51wOfngyX74dYjJRw6hibqw07vGHvvSYPD_f3f2-_F7hG0k0GwWf2Xv4g7LyeZNyn_vR28s6GIbd3e-78_TapNIEo_tJZ-2fTViOmkQYOnM=@
> > protonmail.com>
> >
> > Content-Type: text/plain; charset="utf-8"
> >
> > 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-0001.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-0001.sig
> > >
> >
> > ------------------------------
> >
> > Subject: Digest Footer
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >
> >
> > ------------------------------
> >
> > End of Beginners Digest, Vol 143, Issue 12
> > ******************************************
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.haskell.org/pipermail/beginners/attachments/20200525/002f0b3f/attachment-0001.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
> ------------------------------
>
> End of Beginners Digest, Vol 143, Issue 13
> ******************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200526/4607f2a5/attachment-0001.html>
More information about the Beginners
mailing list