<div dir="ltr"><div>Hi,</div><div><br></div><div>So I tried it out, and there're a couple of things:</div><div>- 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`).</div><div>- 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`:</div><div>```</div><div>dist-newstyle/build/x86_64-linux/ghc-8.6.5/HaskellForeignCPP-0.0.1/build/cpp-src/some.o:     file format elf64-x86-64</div><br>SYMBOL TABLE:<br>0000000000000000 l    df *ABS*   0000000000000000 some.cpp<br>0000000000000000 l    d  .text    0000000000000000 .text<br>0000000000000000 l    d  .data       0000000000000000 .data<br>0000000000000000 l    d  .bss        0000000000000000 .bss<br>0000000000000000 l    d  .note.GNU-stack      0000000000000000 .note.GNU-stack<br>0000000000000000 l    d  .eh_frame 0000000000000000 .eh_frame<br>0000000000000000 l    d  .comment        0000000000000000 .comment<br>0000000000000000 g     F .text     0000000000000006 _Z8multiplyii<div>```</div><div>so you just need to wrap every exposed function in `extern "C"` blocks, like it's explained here: <a href="https://www.geeksforgeeks.org/extern-c-in-c/">https://www.geeksforgeeks.org/extern-c-in-c/</a>.</div><div><br></div><div>With `extern "C"` and removing `static`:</div><div>```</div><div>dist-newstyle/build/x86_64-linux/ghc-8.6.5/HaskellForeignCPP-0.0.1/build/cpp-src/some.o:     file format elf64-x86-64<br><br>SYMBOL TABLE:<br>0000000000000000 l    df *ABS* 0000000000000000 some.cpp<br>0000000000000000 l    d  .text    0000000000000000 .text<br>0000000000000000 l    d  .data       0000000000000000 .data<br>0000000000000000 l    d  .bss        0000000000000000 .bss<br>0000000000000000 l    d  .note.GNU-stack      0000000000000000 .note.GNU-stack<br>0000000000000000 l    d  .eh_frame 0000000000000000 .eh_frame<br>0000000000000000 l    d  .comment        0000000000000000 .comment<br>0000000000000000 g     F .text     0000000000000006 multiply<br></div><div>```</div><div>...and everything builds fine.</div><div><br></div><div>Best,</div><div><br></div><div>toz</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, May 26, 2020 at 5:03 AM <<a href="mailto:beginners-request@haskell.org">beginners-request@haskell.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Send Beginners mailing list submissions to<br>
        <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
        <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
or, via email, send a message with subject or body 'help' to<br>
        <a href="mailto:beginners-request@haskell.org" target="_blank">beginners-request@haskell.org</a><br>
<br>
You can reach the person managing the list at<br>
        <a href="mailto:beginners-owner@haskell.org" target="_blank">beginners-owner@haskell.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than "Re: Contents of Beginners digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
   1. Re:  Linking C++ in Cabal - repository Link (Leonhard Applis)<br>
   2. Re:  Linking C++ in Cabal (鲍凯文)<br>
<br>
<br>
----------------------------------------------------------------------<br>
<br>
Message: 1<br>
Date: Mon, 25 May 2020 13:00:03 +0000<br>
From: Leonhard Applis <<a href="mailto:Leonhard.Applis@protonmail.com" target="_blank">Leonhard.Applis@protonmail.com</a>><br>
To: "<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>" <<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>><br>
Subject: Re: [Haskell-beginners] Linking C++ in Cabal - repository<br>
        Link<br>
Message-ID:<br>
        <iF7eLvkM4HniAVDyO09U-O3y48pPEAciKs5zjKAtajj8lldf1g8Tp5UHi3-HiT2GMc15GBi5JgVgkRXr4KQ8Y8p6i0CuuAU-RLh-g2t57uQ=@<a href="http://protonmail.com" rel="noreferrer" target="_blank">protonmail.com</a>><br>
<br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Ups, in my mailing list the links have disappeared.<br>
Just in case:  <br>
<br>
<br>
The minimal reproducing repository: <br>
<br>
<a href="https://github.com/Twonki/HaskellForeingCPPMinimal" rel="noreferrer" target="_blank">https://github.com/Twonki/HaskellForeingCPPMinimal</a><br>
<br>
The repository from Aditya Siram with C and makefile: <br>
<br>
<a href="https://github.com/deech/CPlusPlusBindings" rel="noreferrer" target="_blank">https://github.com/deech/CPlusPlusBindings</a><br>
<br>
best<br>
Leonhard<br>
<br>
<br>
<br>
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐<br>
On Monday, May 25, 2020 2:00 PM, <<a href="mailto:beginners-request@haskell.org" target="_blank">beginners-request@haskell.org</a>> wrote:<br>
<br>
> Send Beginners mailing list submissions to<br>
> <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><br>
> <br>
<br>
> To subscribe or unsubscribe via the World Wide Web, visit<br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
> or, via email, send a message with subject or body 'help' to<br>
> <a href="mailto:beginners-request@haskell.org" target="_blank">beginners-request@haskell.org</a><br>
> <br>
<br>
> You can reach the person managing the list at<br>
> <a href="mailto:beginners-owner@haskell.org" target="_blank">beginners-owner@haskell.org</a><br>
> <br>
<br>
> When replying, please edit your Subject line so it is more specific<br>
> than "Re: Contents of Beginners digest..."<br>
> <br>
<br>
> Today's Topics:<br>
> <br>
<br>
> 1.  Linking C++ in Cabal (Leonhard Applis)<br>
>     <br>
<br>
> <br>
<br>
> Message: 1<br>
> Date: Sun, 24 May 2020 16:30:22 +0000<br>
> From: Leonhard Applis <a href="mailto:Leonhard.Applis@protonmail.com" target="_blank">Leonhard.Applis@protonmail.com</a><br>
> To: "<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>" <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><br>
> Subject: [Haskell-beginners] Linking C++ in Cabal<br>
> Message-ID:<br>
> 51wOfngyX74dYjJRw6hibqw07vGHvvSYPD_f3f2-_F7hG0k0GwWf2Xv4g7LyeZNyn_vR28s6GIbd3e-78_TapNIEo_tJZ-2fTViOmkQYOnM=@<a href="http://protonmail.com" rel="noreferrer" target="_blank">protonmail.com</a><br>
> <br>
<br>
> Content-Type: text/plain; charset="utf-8"<br>
> <br>
<br>
> Hello,<br>
> <br>
<br>
> I want to use a foreign C++ Function in a Haskell Library, and the Library in a Haskell Program.<br>
> The C++ function is separate in a file and I have a header file. <br>
> I made a minimal reproducing repository.<br>
> <br>
<br>
> The closes I have found is this repository which wraps the c++ in c and makes a full library, needing a makefile etc.<br>
> So while I think I'd get it working like that, it looks to be quite a lot to do for my simple task.<br>
> <br>
<br>
> the repository structure looks as follows:<br>
> <br>
<br>
> Projekt<br>
> <br>
<br>
> -   cpp-src<br>
> -   some.cpp<br>
> -   some.h<br>
> -   lib-src<br>
> -   CPPLib.hs<br>
> -   program-src<br>
> -   program.hs<br>
> -   project.cabal<br>
>     <br>
<br>
>     And my (currently failing) project.cabal the following:<br>
>     <br>
<br>
>     cabal-version: 3.0<br>
>     name: cpp_lib<br>
>     [...]<br>
>     library<br>
>     exposed-modules: CPPLib<br>
>     other-extensions: ForeignFunctionInterface<br>
>     build-depends: base<br>
>     hs-source-dirs: lib-src<br>
>     default-language: Haskell2010<br>
>     include-dirs: ./cpp-src<br>
>     cxx-sources: ./cpp-src/some.cpp<br>
>     install-includes: ./cpp-src/some.h<br>
>     <br>
<br>
>     executable CPPApp<br>
>     main-is: program.hs<br>
>     default-language: Haskell2010<br>
>     build-depends: HaskellForeignCPP,<br>
>     base<br>
>     hs-source-dirs: program-src<br>
>     <br>
<br>
>     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.<br>
>     <br>
<br>
> <br>
<br>
> The library compiles fine, but when building the executable it fails telling me<br>
> <br>
<br>
> collect2: error: ld returned 1 exit status<br>
> `gcc' failed in phase`Linker'. (Exit code: 1)<br>
> <br>
<br>
> Can someone help me out?<br>
> I find very little documentation on this topic.<br>
> <br>
<br>
> 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.<br>
> <br>
<br>
> best<br>
> Leonhard<br>
> -------------- next part --------------<br>
> An HTML attachment was scrubbed...<br>
> URL: <a href="http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment-0001.html" rel="noreferrer" target="_blank">http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment-0001.html</a><br>
> -------------- next part --------------<br>
> A non-text attachment was scrubbed...<br>
> Name: signature.asc<br>
> Type: application/pgp-signature<br>
> Size: 477 bytes<br>
> Desc: OpenPGP digital signature<br>
> URL: <a href="http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment-0001.sig" rel="noreferrer" target="_blank">http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment-0001.sig</a><br>
> <br>
<br>
> --<br>
> <br>
<br>
> Subject: Digest Footer<br>
> <br>
<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
> <br>
<br>
> <br>
<br>
> ---------------------------------------------------------------------------------------------------------<br>
> <br>
<br>
> End of Beginners Digest, Vol 143, Issue 12<br>
<br>
-------------- next part --------------<br>
A non-text attachment was scrubbed...<br>
Name: signature.asc<br>
Type: application/pgp-signature<br>
Size: 477 bytes<br>
Desc: OpenPGP digital signature<br>
URL: <<a href="http://mail.haskell.org/pipermail/beginners/attachments/20200525/0e889731/attachment-0001.sig" rel="noreferrer" target="_blank">http://mail.haskell.org/pipermail/beginners/attachments/20200525/0e889731/attachment-0001.sig</a>><br>
<br>
------------------------------<br>
<br>
Message: 2<br>
Date: Mon, 25 May 2020 16:45:09 -0700<br>
From: 鲍凯文 <<a href="mailto:traqueofziche@gmail.com" target="_blank">traqueofziche@gmail.com</a>><br>
To: <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><br>
Subject: Re: [Haskell-beginners] Linking C++ in Cabal<br>
Message-ID:<br>
        <CAMjcG+FWPZtkM7soC34VOdSBQ+ArdWY2Bpk2Wm=<a href="mailto:XNqEdg3ox2Q@mail.gmail.com" target="_blank">XNqEdg3ox2Q@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Hi,<br>
<br>
Would it be possible to upload your full repo somewhere so people could see<br>
everything and possibly reproduce this?<br>
<br>
Thanks,<br>
<br>
toz<br>
<br>
On Mon, May 25, 2020 at 5:03 AM <<a href="mailto:beginners-request@haskell.org" target="_blank">beginners-request@haskell.org</a>> wrote:<br>
<br>
> Send Beginners mailing list submissions to<br>
>         <a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a><br>
><br>
> To subscribe or unsubscribe via the World Wide Web, visit<br>
>         <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
> or, via email, send a message with subject or body 'help' to<br>
>         <a href="mailto:beginners-request@haskell.org" target="_blank">beginners-request@haskell.org</a><br>
><br>
> You can reach the person managing the list at<br>
>         <a href="mailto:beginners-owner@haskell.org" target="_blank">beginners-owner@haskell.org</a><br>
><br>
> When replying, please edit your Subject line so it is more specific<br>
> than "Re: Contents of Beginners digest..."<br>
><br>
><br>
> Today's Topics:<br>
><br>
>    1.  Linking C++ in Cabal (Leonhard Applis)<br>
><br>
><br>
> ----------------------------------------------------------------------<br>
><br>
> Message: 1<br>
> Date: Sun, 24 May 2020 16:30:22 +0000<br>
> From: Leonhard Applis <<a href="mailto:Leonhard.Applis@protonmail.com" target="_blank">Leonhard.Applis@protonmail.com</a>><br>
> To: "<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>" <<a href="mailto:beginners@haskell.org" target="_blank">beginners@haskell.org</a>><br>
> Subject: [Haskell-beginners] Linking C++ in Cabal<br>
> Message-ID:<br>
><br>
> <51wOfngyX74dYjJRw6hibqw07vGHvvSYPD_f3f2-_F7hG0k0GwWf2Xv4g7LyeZNyn_vR28s6GIbd3e-78_TapNIEo_tJZ-2fTViOmkQYOnM=@<br>
> <a href="http://protonmail.com" rel="noreferrer" target="_blank">protonmail.com</a>><br>
><br>
> Content-Type: text/plain; charset="utf-8"<br>
><br>
> Hello,<br>
><br>
> I want to use a foreign C++ Function in a Haskell Library, and the Library<br>
> in a Haskell Program.<br>
> The C++ function is separate in a file and I have a header file.<br>
> I made a minimal reproducing repository.<br>
><br>
> The closes I have found is this repository which wraps the c++ in c and<br>
> makes a full library, needing a makefile etc.<br>
> So while I think I'd get it working like that, it looks to be quite a lot<br>
> to do for my simple task.<br>
><br>
> the repository structure looks as follows:<br>
><br>
> Projekt<br>
><br>
> -   cpp-src<br>
><br>
> -   some.cpp<br>
> -   some.h<br>
><br>
> -   lib-src<br>
><br>
> -   CPPLib.hs<br>
><br>
> -   program-src<br>
><br>
> -   program.hs<br>
><br>
> -   project.cabal<br>
><br>
><br>
><br>
> And my (currently failing) project.cabal the following:<br>
><br>
> cabal-version:  3.0<br>
> name: cpp_lib<br>
> [...]<br>
> library<br>
> exposed-modules: CPPLib<br>
> other-extensions: ForeignFunctionInterface<br>
> build-depends: base<br>
> hs-source-dirs: lib-src<br>
> default-language: Haskell2010<br>
> include-dirs: ./cpp-src<br>
> cxx-sources: ./cpp-src/some.cpp<br>
> install-includes: ./cpp-src/some.h<br>
><br>
> executable CPPApp<br>
> main-is: program.hs<br>
> default-language: Haskell2010<br>
> build-depends: HaskellForeignCPP,<br>
> base<br>
> hs-source-dirs: program-src<br>
><br>
> The some.cpp is trivial for this case lets assume its a function multiply<br>
> :: Int -> Int -> Int, and both .cpp and .h are well-formed and don't have<br>
> further dependencies.<br>
><br>
> The library compiles fine, but when building the executable it fails<br>
> telling me<br>
><br>
> collect2: error: ld returned 1 exit status<br>
> `gcc' failed in phase `Linker'. (Exit code: 1)<br>
><br>
> Can someone help me out?<br>
> I find very little documentation on this topic.<br>
><br>
> I know there is Inline-CPP which would help as well, but I just very much<br>
> like the idea of having a nice separate file and interface for modularity.<br>
><br>
> best<br>
> Leonhard<br>
> -------------- next part --------------<br>
> An HTML attachment was scrubbed...<br>
> URL: <<br>
> <a href="http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment-0001.html" rel="noreferrer" target="_blank">http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment-0001.html</a><br>
> ><br>
> -------------- next part --------------<br>
> A non-text attachment was scrubbed...<br>
> Name: signature.asc<br>
> Type: application/pgp-signature<br>
> Size: 477 bytes<br>
> Desc: OpenPGP digital signature<br>
> URL: <<br>
> <a href="http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment-0001.sig" rel="noreferrer" target="_blank">http://mail.haskell.org/pipermail/beginners/attachments/20200524/9019cbc0/attachment-0001.sig</a><br>
> ><br>
><br>
> ------------------------------<br>
><br>
> Subject: Digest Footer<br>
><br>
> _______________________________________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
><br>
><br>
> ------------------------------<br>
><br>
> End of Beginners Digest, Vol 143, Issue 12<br>
> ******************************************<br>
><br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.haskell.org/pipermail/beginners/attachments/20200525/002f0b3f/attachment-0001.html" rel="noreferrer" target="_blank">http://mail.haskell.org/pipermail/beginners/attachments/20200525/002f0b3f/attachment-0001.html</a>><br>
<br>
------------------------------<br>
<br>
Subject: Digest Footer<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br>
<br>
------------------------------<br>
<br>
End of Beginners Digest, Vol 143, Issue 13<br>
******************************************<br>
</blockquote></div></div>