www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Just starting with D (linking with C++)

reply sivakon <sivakon outlook.com> writes:
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
Oct 27
next sibling parent reply Joakim <dlang joakim.fea.st> writes:
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/
Oct 27
parent reply sivakon <sivakon outlook.com> writes:
On Friday, 27 October 2017 at 17:21:39 UTC, Joakim wrote:
 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/
Just used this! Got this error! sample.o: In function `foo(int, int, int)': sample.cpp:(.text+0x17): undefined reference to `std::cout'
Oct 27
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/27/17 1:43 PM, sivakon wrote:
 On Friday, 27 October 2017 at 17:21:39 UTC, Joakim wrote:
 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/
Just used this! Got this error! sample.o: In function `foo(int, int, int)': sample.cpp:(.text+0x17): undefined reference to `std::cout'
You need to link against the C++ standard library. On my system, it appears to be -lc++. So in order to pass this value to the linker, use the dmd parameter -L-lc++ In order to find out what the name of the c++ library is, you need to compile a C++ program with the -v option. The linker step will show the library parameters you need to send to dmd. -Steve
Oct 27
prev sibling parent Joakim <dlang joakim.fea.st> writes:
On Friday, 27 October 2017 at 17:43:08 UTC, sivakon wrote:
 On Friday, 27 October 2017 at 17:21:39 UTC, Joakim wrote:
 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/
Just used this! Got this error! sample.o: In function `foo(int, int, int)': sample.cpp:(.text+0x17): undefined reference to `std::cout'
Sorry, just responded quickly from my tablet, didn't try it out first. There's actually a variant of this sample that is run as a test from the dmd compiler testsuite, so it is checked hundreds of times a day for pending pull requests, on all officially supported platforms by the auto-tester: https://auto-tester.puremagic.com/pulls.ghtml?projectid=1 https://github.com/dlang/dmd/blob/master/test/runnable/extra-files/cppb.cpp#L41 https://github.com/dlang/dmd/blob/master/test/runnable/cppa.d#L25 Looking at the D script that runs each compiler test, it adds the following flag for all C++ objects: https://github.com/dlang/dmd/blob/master/test/d_do_test.d#L499 So adding that flag got your example to work for me: dmd -L-lstdc++ foo.d Sample.o
Oct 27
prev sibling parent reply codephantom <me noyb.com> writes:
On Friday, 27 October 2017 at 17:14:20 UTC, sivakon wrote:
 I want to use C++ libraries for machine learning and deep 
 learning. How do I add C++ libraries to my d code.
on FreeBSD, I use: for C static binding: ---------------------- clang -c sample.c dmd -L-lc foo.d sample.o or ldc -L-lc foo.d sample.o for c++ static binding: ----------------------- clang++ -c sample.cpp dmd -L-lc++ foo.d sample.o or ldc -L-lc++ foo.d sample.o There is nice article here that was pretty interesting too: https://www.gamedev.net/blogs/entry/2254003-binding-d-to-c/
Oct 27
parent sivakon <sivakon outlook.com> writes:
On Saturday, 28 October 2017 at 02:20:42 UTC, codephantom wrote:
 On Friday, 27 October 2017 at 17:14:20 UTC, sivakon wrote:
 I want to use C++ libraries for machine learning and deep 
 learning. How do I add C++ libraries to my d code.
on FreeBSD, I use: for C static binding: ---------------------- clang -c sample.c dmd -L-lc foo.d sample.o or ldc -L-lc foo.d sample.o for c++ static binding: ----------------------- clang++ -c sample.cpp dmd -L-lc++ foo.d sample.o or ldc -L-lc++ foo.d sample.o There is nice article here that was pretty interesting too: https://www.gamedev.net/blogs/entry/2254003-binding-d-to-c/
Thanks! That worked like a charm. Now I can use dlang confidently. Any tips to make it faster cflags etc.
Oct 28