www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Interfacing D with C and C++

reply "Scott Wilson" <scott_wilson_3rd yahoo.com> writes:
Hello, world. Brand new poster here though I posted similar 
messages in other language forums. I hope this is the right place 
to ask because my question is half about existing stuff and half 
about prospective work.

I am considering starting with D amid a C++ code base. New D code 
would need to somehow integrate with the existing C++ code. The 
C++ code is the expected melange of free functions, classes, what 
have you and uses STL and Boost (also other libs less 
prominently). Allow me to ask:

1. What is the current support for calling C/C++ free functions 
from D? What level of mangling is supported? What data types can 
be passed without translation from D to C/C++?

2. How about template functions? Is it possible to call a C++ 
template function from D?

3. How can a C++ object be used from D? Can C++ methods be called 
from D? The question applies to value types - no virtuals - and 
polymorphic types with virtuals, inheritance etc. And of course 
simple C structs.

4. How about template objects? One issue is that many C++ 
interfaces pass std::string and std::map<..., ...> as parameters. 
How feasible is to manipulate such objects in D?

5. How about the other way? Can a C/C++ function call a D 
function?

I would appreciate any pointers you have to how-to materials. In 
equal measure I am interested in plans to address such issues in 
the foreseeable future. Thankyou.


Scott
Aug 23 2014
next sibling parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
Welcome!

 1. What is the current support for calling C/C++ free functions 
 from D? What level of mangling is supported? What data types 
 can be passed without translation from D to C/C++?
 3. How can a C++ object be used from D? Can C++ methods be 
 called from D? The question applies to value types - no 
 virtuals - and polymorphic types with virtuals, inheritance 
 etc. And of course simple C structs.
 5. How about the other way? Can a C/C++ function call a D 
 function?
http://dlang.org/cpp_interface.html
 2. How about template functions? Is it possible to call a C++ 
 template function from D?
 4. How about template objects? One issue is that many C++ 
 interfaces pass std::string and std::map<..., ...> as 
 parameters. How feasible is to manipulate such objects in D?
Not yet. http://forum.dlang.org/thread/lslofn$2iro$1 digitalmars.com
Aug 23 2014
prev sibling next sibling parent reply "bachmeier" <no spam.net> writes:
On Saturday, 23 August 2014 at 22:46:11 UTC, Scott Wilson wrote:
 Hello, world. Brand new poster here though I posted similar 
 messages in other language forums. I hope this is the right 
 place to ask because my question is half about existing stuff 
 and half about prospective work.

 I am considering starting with D amid a C++ code base. New D 
 code would need to somehow integrate with the existing C++ 
 code. The C++ code is the expected melange of free functions, 
 classes, what have you and uses STL and Boost (also other libs 
 less prominently). Allow me to ask:

 1. What is the current support for calling C/C++ free functions 
 from D? What level of mangling is supported? What data types 
 can be passed without translation from D to C/C++?

 2. How about template functions? Is it possible to call a C++ 
 template function from D?

 3. How can a C++ object be used from D? Can C++ methods be 
 called from D? The question applies to value types - no 
 virtuals - and polymorphic types with virtuals, inheritance 
 etc. And of course simple C structs.

 4. How about template objects? One issue is that many C++ 
 interfaces pass std::string and std::map<..., ...> as 
 parameters. How feasible is to manipulate such objects in D?

 5. How about the other way? Can a C/C++ function call a D 
 function?

 I would appreciate any pointers you have to how-to materials. 
 In equal measure I am interested in plans to address such 
 issues in the foreseeable future. Thankyou.


 Scott
I'm only able to answer (5). I regularly create shared libraries on Linux and call them from C. It's very simple (for what I've done anyway). The info on this page http://dlang.org/interfaceToC should get you started. http://dlang.org/dll-linux.html#dso9 explains compilation. http://forum.dlang.org/group/digitalmars.D.learn is a friendly place to ask if you've got questions. This recent post http://forum.dlang.org/post/fxdqpmfcbskvtcafzfcp forum.dlang.org and some of the replies explain where things are headed and difficulties with C++ interoperability.
Aug 23 2014
parent reply "Scott Wilson" <scott_wilson_3rd yahoo.com> writes:
Thank you all for responding. I have run the following 
experiment. Trying to call a method of std::allocator<int> I am 
sure I am doing something wrong. Please do tell and apologies for 
my noobiness.

// test.d
extern(C++, std)
{
     struct allocator(T)
     {
         alias size_type = size_t;
         void deallocate(void*, size_type);
     }
}

void testing(std.allocator!int * pa)
{
     pa.deallocate(null, 0);
}

void main(string[] args)
{
}
// end of test.d

There must be an object file in C++ to link. I wrote.

// test.cpp
#include <memory>
template struct std::allocator<int>;
// end of test.cpp

Commands used were

g++ -c test2.cpp
dmd test.d test2.o

There are these errors

Undefined symbols for architecture x86_64:
   "std::allocator<int>::deallocate(void*, unsigned long)", 
referenced from:
       _D4test3funFPS4test3std16__T9allocatorTiZ9allocatorZv in 
test.o
   "operator delete(void*)", referenced from:
       std::__1::allocator<int>::deallocate(int*, unsigned long) 
in test2.o
   "operator new(unsigned long)", referenced from:
       std::__1::allocator<int>::allocate(unsigned long, void 
const*) in test2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)
--- errorlevel 1

To zero in on the matter I wrote the implementation by hand. 
Changed test2.cpp with

// test2.cpp
namespace std
{
     template <class T> struct allocator
     {
         void deallocate(void*, unsigned long) {}
     };
     template struct allocator<int>;
}
// end of test2.cpp

Only fewer errors.

Undefined symbols for architecture x86_64:
   "std::allocator<int>::deallocate(void*, unsigned long)", 
referenced from:
       _D4test3funFPS4test3std16__T9allocatorTiZ9allocatorZv in 
test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)
--- errorlevel 1

Could you tell me how I could get this to work?


Scott

On Sunday, 24 August 2014 at 02:32:22 UTC, bachmeier wrote:
 On Saturday, 23 August 2014 at 22:46:11 UTC, Scott Wilson wrote:
 Hello, world. Brand new poster here though I posted similar 
 messages in other language forums. I hope this is the right 
 place to ask because my question is half about existing stuff 
 and half about prospective work.

 I am considering starting with D amid a C++ code base. New D 
 code would need to somehow integrate with the existing C++ 
 code. The C++ code is the expected melange of free functions, 
 classes, what have you and uses STL and Boost (also other libs 
 less prominently). Allow me to ask:

 1. What is the current support for calling C/C++ free 
 functions from D? What level of mangling is supported? What 
 data types can be passed without translation from D to C/C++?

 2. How about template functions? Is it possible to call a C++ 
 template function from D?

 3. How can a C++ object be used from D? Can C++ methods be 
 called from D? The question applies to value types - no 
 virtuals - and polymorphic types with virtuals, inheritance 
 etc. And of course simple C structs.

 4. How about template objects? One issue is that many C++ 
 interfaces pass std::string and std::map<..., ...> as 
 parameters. How feasible is to manipulate such objects in D?

 5. How about the other way? Can a C/C++ function call a D 
 function?

 I would appreciate any pointers you have to how-to materials. 
 In equal measure I am interested in plans to address such 
 issues in the foreseeable future. Thankyou.


 Scott
I'm only able to answer (5). I regularly create shared libraries on Linux and call them from C. It's very simple (for what I've done anyway). The info on this page http://dlang.org/interfaceToC should get you started. http://dlang.org/dll-linux.html#dso9 explains compilation. http://forum.dlang.org/group/digitalmars.D.learn is a friendly place to ask if you've got questions. This recent post http://forum.dlang.org/post/fxdqpmfcbskvtcafzfcp forum.dlang.org and some of the replies explain where things are headed and difficulties with C++ interoperability.
Aug 23 2014
next sibling parent reply ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Sun, 24 Aug 2014 04:31:55 +0000
Scott Wilson via Digitalmars-d <digitalmars-d puremagic.com> wrote:

there is NO support for instantiating C++ templates in D code. and you
can't write C++ template specialization code in D.
Aug 23 2014
parent reply "Scott Wilson" <scott_wilson_3rd yahoo.com> writes:
The D code is not instantiating (but I may be wrong). I think 
writing template struct allocator<int>; on the C++ side 
instantiates and the D code only links with that instantiation. I 
reckon I'm out of my depth here.

Scott

On Sunday, 24 August 2014 at 04:47:38 UTC, ketmar via 
Digitalmars-d wrote:
 On Sun, 24 Aug 2014 04:31:55 +0000
 Scott Wilson via Digitalmars-d <digitalmars-d puremagic.com> 
 wrote:

 there is NO support for instantiating C++ templates in D code. 
 and you
 can't write C++ template specialization code in D.
Aug 23 2014
parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Sun, 24 Aug 2014 05:15:14 +0000
Scott Wilson via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 The D code is not instantiating (but I may be wrong).
sorry, it was my fault. i somehow misread your code.
Aug 23 2014
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 8/23/2014 9:31 PM, Scott Wilson wrote:
 Could you tell me how I could get this to work?
The function is mangled as: C++: _ZNSaIiE10deallocateEPvm D: _ZN3std9allocatorIiE10deallocateEPvm Looks like D gets it wrong.
Aug 23 2014
next sibling parent "Scott Wilson" <scott_wilson_3rd yahoo.com> writes:
On Sunday, 24 August 2014 at 04:59:04 UTC, Walter Bright wrote:
 On 8/23/2014 9:31 PM, Scott Wilson wrote:
 Could you tell me how I could get this to work?
The function is mangled as: C++: _ZNSaIiE10deallocateEPvm D: _ZN3std9allocatorIiE10deallocateEPvm Looks like D gets it wrong.
Thank you for your attention. This means there is a bug in the compiler? Do you think there is a simple fix to this or a workaround? Unfortunately my compiler fu is lackadaisical. Scott
Aug 23 2014
prev sibling parent "Kagamin" <spam here.lot> writes:
Looks like std::allocator got reduced to just Sa. What a hack.
Aug 24 2014
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 8/23/2014 3:46 PM, Scott Wilson wrote:
 1. What is the current support for calling C/C++ free functions from D? What
 level of mangling is supported? What data types can be passed without
 translation from D to C/C++?
D's native types can be used to call C/C++ free functions directly without translation. Structs are the same.
 2. How about template functions? Is it possible to call a C++ template function
 from D?
Yes.
 3. How can a C++ object be used from D? Can C++ methods be called from D? The
 question applies to value types - no virtuals - and polymorphic types with
 virtuals, inheritance etc. And of course simple C structs.
Simple structs can be passed back and forth without modification. Classes with single inheritance can be passed by reference back and forth.
 4. How about template objects? One issue is that many C++ interfaces pass
 std::string and std::map<..., ...> as parameters. How feasible is to manipulate
 such objects in D?
Check out the following update to the compiler https://github.com/D-Programming-Language/dmd/pull/3895 which makes it possible for D to interface with C++ std functions.
 5. How about the other way? Can a C/C++ function call a D function?
Yes.
Aug 26 2014