Just starting with D (linking with C++)

Joakim dlang at joakim.fea.st
Fri Oct 27 17:21:39 UTC 2017


On Friday, 27 October 2017 at 17:14:20 UTC, sivakon wrote:
> Hi,
>
> Just started to work with D. Great language.
>
> I want to use C++ libraries for machine learning and deep 
> learning. How do I add C++ libraries to my d code.
>
> For example,
>
> //sample.cpp
> #include <iostream>
>
> using namespace std;
>
> int foo(int i, int j, int k)
> {
>     cout << "i = " << i << endl;
>     cout << "j = " << j << endl;
>     cout << "k = " << k << endl;
>
>     return 7;
> }
>
>
> //foo.d
> extern (C++) int foo(int i, int j, int k);
>
> void main()
> {
> 	foo(1,2,3);
> }
>
>
> How do I compile and link each of them to produce the result?
> dmd -c foo.d
> gcc -c Sample.cpp
>
> Both of them get me .o files. Where do I go from there?
>
> Thanks,
> Siv

This should work:

dmd foo.d Sample.o

Just like the C examples from the D blog:

https://dlang.org/blog/2017/10/25/dmd-windows-and-c/


More information about the Digitalmars-d-learn mailing list