www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C++ interface.

reply "monnoroch" <monnoroch gmail.com> writes:
Could anyone explain, why D cannot interface C++ functions in 
namespaces like that:

namespace A {
    struct S {};
    struct T {};
    S* get(T* v);
}

Because i just ddon't get it: namespaces are just a part of C++ 
name mangling, they are not something special.

Support of this kind of functions would make it much easier to 
integrate D in existing C++ repos, which is the key to success 
(sadly, though).
Apr 01 2014
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/01/2014 10:32 AM, monnoroch wrote:

 namespaces are just a part of C++ name mangling, they are not
 something special.

However, the mangling itself is not standardized. Ali
Apr 01 2014
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 4/1/2014 10:38 AM, Ali Çehreli wrote:
 However, the mangling itself is not standardized.

That isn't the problem - dmd emits custom mangling for each platform. The problem is "how to specify C++ namespaces in D".
Apr 01 2014
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2014-04-01 20:37, Walter Bright wrote:
 On 4/1/2014 10:38 AM, Ali Çehreli wrote:
 However, the mangling itself is not standardized.

That isn't the problem - dmd emits custom mangling for each platform. The problem is "how to specify C++ namespaces in D".

with UDA's, of course :) namespace("foo::bar") { extern (C++): void foo (); void bar (); } -- /Jacob Carlborg
Apr 01 2014
prev sibling next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 4/1/2014 12:29 PM, monnoroch wrote:
 It would first try to search nmr_initialize itself, and if thre
 is no, it would try to find nmr::initialize. Recursevly y the
 number of ::-s.

The D compiler has nowhere to search for nmr::initialize.
Apr 01 2014
parent "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"Asman01"  wrote in message news:qltrgqselahxazgrqdxo forum.dlang.org...

 Why do we don't use our own linker? better, what exactly do we need to 
 begin using the Daniel's linker instead of gcc's?

Well, someone would need to add ELF support since it's currently OMF/COFF only. While a linker technically _could_ help here with some magic symbol rewriting, it's far from the most practical way to get namespaces working.
Apr 01 2014
prev sibling next sibling parent Mike Parker <aldacron gmail.com> writes:
On 4/2/2014 7:30 PM, monnoroch wrote:
 On Wednesday, 2 April 2014 at 04:59:11 UTC, Mike Parker wrote:
 People who are concerned about cross-language compatibility should be
 implementing C interfaces to their C++ libraries anyway. Yes, it's a
 nice, convenient feature to have, but ultimately there are more
 important things, IMO.

True. But i find myself with like 10+Gb of c++ sources, are you suggesting that i should write C wrapper for all that code to use D? Or maby you suggesting that i have to make all my (hundreds) collegs write C wrappers for all new code? I really just trying to start D community in company i work, and there is no way to do it without nice integration with old codebase.

What I'm suggesting is that there are a lot of people with a lot of different priorities and still a lot of work to be done just to get the core language features where they need to be. D already has interop with C and that's been extremely important. While I think it would be fantastic to have fully-functional C++ interop so that one could just drop a D module into a project and take off, I wouldn't expect that to be a major priority at this time.
Apr 02 2014
prev sibling parent "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"DanielKozák"  wrote in message 
news:mailman.36.1396437697.19942.digitalmars-d puremagic.com...

 I thought it was a stated goal of D to have C++ interoperability
 for the same compiler suite?
 E.g. dmd/dmc++, g++/gdc, ldc/clang…

This would be awesome. But it would be much better if DMD has interoperability with g++ and/or clang too.

It does, on non-windows platforms, although C++ interoperability is closer to a nice-to-have than a 'stated goal'.
Apr 03 2014
prev sibling next sibling parent "monnoroch" <monnoroch gmail.com> writes:
 However, the mangling itself is not standardized.

Yes, i know that, but since dmd already can decrypt the mangling of overloaded functions, it could just do the same for namespaces.
Apr 01 2014
prev sibling next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 1 April 2014 at 17:38:08 UTC, Ali Çehreli wrote:
 On 04/01/2014 10:32 AM, monnoroch wrote:

 namespaces are just a part of C++ name mangling, they are not
 something special.

However, the mangling itself is not standardized. Ali

Is it prohibitively arduous just to emulate a variety of common compilers and control it with a compiler switch?
Apr 01 2014
prev sibling next sibling parent reply "monnoroch" <monnoroch gmail.com> writes:
I mean, it would be just super cool. At my work we have like 
gigabytes of c++ code, and almost all of it in namespaces, if i 
just could write simple extern(C++) declarations for them it 
would be so much easier to start working with D.
Apr 01 2014
parent Jacob Carlborg <doob me.com> writes:
On 2014-04-01 20:01, Rene Zwanenburg wrote:
 On Tuesday, 1 April 2014 at 17:49:21 UTC, monnoroch wrote:
 I mean, it would be just super cool. At my work we have like gigabytes
 of c++ code, and almost all of it in namespaces, if i just could write
 simple extern(C++) declarations for them it would be so much easier to
 start working with D.

If you know how your C++ compiler mangles functions inside namespaces, you should be able to hack something together using pragma mangle. I'm not sure how nice it can be, ideally a UDA (user defined attribute) should be enough, but I've never used them myself so I don't know if this is possible: Cpp("namespace") void foo(int x); or even: Cpp("namespace") { void foo(); void bar(); } where Cpp applies extern(C++) and pragma(mangle, "correctlymangledname"). Anyone knows if this can be done?

That's not currently possible. Have a look at this: http://wiki.dlang.org/DIP50#C.2B.2B_Namespaces_.28issue_7961.29 -- /Jacob Carlborg
Apr 01 2014
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2014-04-01 19:32, monnoroch wrote:
 Could anyone explain, why D cannot interface C++ functions in namespaces
 like that:

 namespace A {
     struct S {};
     struct T {};
     S* get(T* v);
 }

 Because i just ddon't get it: namespaces are just a part of C++ name
 mangling, they are not something special.

I'm pretty sure you can use extern (C++) then manually specify the mangling using pragma(mangle, "mangled_c++_name"). -- /Jacob Carlborg
Apr 01 2014
prev sibling next sibling parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
On Tuesday, 1 April 2014 at 17:49:21 UTC, monnoroch wrote:
 I mean, it would be just super cool. At my work we have like 
 gigabytes of c++ code, and almost all of it in namespaces, if i 
 just could write simple extern(C++) declarations for them it 
 would be so much easier to start working with D.

If you know how your C++ compiler mangles functions inside namespaces, you should be able to hack something together using pragma mangle. I'm not sure how nice it can be, ideally a UDA (user defined attribute) should be enough, but I've never used them myself so I don't know if this is possible: Cpp("namespace") void foo(int x); or even: Cpp("namespace") { void foo(); void bar(); } where Cpp applies extern(C++) and pragma(mangle, "correctlymangledname"). Anyone knows if this can be done?
Apr 01 2014
prev sibling next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 1 April 2014 at 18:37:06 UTC, Walter Bright wrote:
 On 4/1/2014 10:38 AM, Ali Çehreli wrote:
 However, the mangling itself is not standardized.

That isn't the problem - dmd emits custom mangling for each platform. The problem is "how to specify C++ namespaces in D".

uninstantiable struct? (as an aside, it'd be nice to have disable init for making truly uninstantiable structs). Or just use modules, if that can be made to work.
Apr 01 2014
prev sibling next sibling parent "monnoroch" <monnoroch gmail.com> writes:
On Tuesday, 1 April 2014 at 18:37:06 UTC, Walter Bright wrote:
 The problem is "how to specify C++ namespaces in D".

Why don't just use dot? So, that: extern(C++) { void nmr.initialize(int argc, const(char)*[] argv); } Would get converted to whatever void nmr::initialize(int, const(char)*[]) would became mangled in c++. If you do not want to hack parser into doing it, just nmr_initialize would be ok. So, if i write: extern(C++) { void nmr_initialize(int argc, const(char)*[] argv); } It would first try to search nmr_initialize itself, and if thre is no, it would try to find nmr::initialize. Recursevly y the number of ::-s.
Apr 01 2014
prev sibling next sibling parent "monnoroch" <monnoroch gmail.com> writes:
Also you could demangling add instantiated templates. If they 
already exist in object files, not only in headers, why not link 
to them? It seems to me, that it is also just demangling problem.
Apr 01 2014
prev sibling next sibling parent "monnoroch" <monnoroch gmail.com> writes:
On Tuesday, 1 April 2014 at 18:37:06 UTC, Walter Bright wrote:
 The problem is "how to specify C++ namespaces in D".

Truth is: right now, any way is better, than don't do that, since it seems that the c++ linking problem stops alot of people to use D. C++ name mangling isn't standartized anyway, so whatever the solution will be, people will understand both syntax and possible lack of backward compaility.
Apr 01 2014
prev sibling next sibling parent "monnoroch" <monnoroch gmail.com> writes:
On Tuesday, 1 April 2014 at 19:51:25 UTC, Walter Bright wrote:
 The D compiler has nowhere to search for nmr::initialize.

Oops, forgot, that it uses gcc to link... Then maby nmr__initialize would be ok? I mean, nobody uses two underscores in the code.
Apr 01 2014
prev sibling next sibling parent "Asman01" <jckj33 gmail.com> writes:
On Tuesday, 1 April 2014 at 19:59:03 UTC, monnoroch wrote:
 On Tuesday, 1 April 2014 at 19:51:25 UTC, Walter Bright wrote:
 The D compiler has nowhere to search for nmr::initialize.

Oops, forgot, that it uses gcc to link... Then maby nmr__initialize would be ok? I mean, nobody uses two underscores in the code.

Why do we don't use our own linker? better, what exactly do we need to begin using the Daniel's linker instead of gcc's? I see this like a big deal but the guy didn't get much feedback from his announce.
Apr 01 2014
prev sibling next sibling parent "monnoroch" <monnoroch gmail.com> writes:
On Tuesday, 1 April 2014 at 20:26:28 UTC, Asman01 wrote:
 Why do we don't use our own linker?

The whole point of a question was to ask for a feature, that would boost D usage in case of existing c++ codebase. I doubt that new linker will be ready and stable for both D and c/c++ object files any time soon. For instance, i have to link D object files with gcc so, i couldn't benefit from new linker anyway.
Apr 01 2014
prev sibling next sibling parent "Mike Parker" <aldacron gmail.com> writes:
On Tuesday, 1 April 2014 at 19:38:11 UTC, monnoroch wrote:
 On Tuesday, 1 April 2014 at 18:37:06 UTC, Walter Bright wrote:
 The problem is "how to specify C++ namespaces in D".

Truth is: right now, any way is better, than don't do that, since it seems that the c++ linking problem stops alot of people to use D. C++ name mangling isn't standartized anyway, so whatever the solution will be, people will understand both syntax and possible lack of backward compaility.

People who are concerned about cross-language compatibility should be implementing C interfaces to their C++ libraries anyway. Yes, it's a nice, convenient feature to have, but ultimately there are more important things, IMO.
Apr 01 2014
prev sibling next sibling parent "monnoroch" <monnoroch gmail.com> writes:
On Wednesday, 2 April 2014 at 04:59:11 UTC, Mike Parker wrote:
 People who are concerned about cross-language compatibility 
 should be implementing C interfaces to their C++ libraries 
 anyway. Yes, it's a nice, convenient feature to have, but 
 ultimately there are more important things, IMO.

True. But i find myself with like 10+Gb of c++ sources, are you suggesting that i should write C wrapper for all that code to use D? Or maby you suggesting that i have to make all my (hundreds) collegs write C wrappers for all new code? I really just trying to start D community in company i work, and there is no way to do it without nice integration with old codebase.
Apr 02 2014
prev sibling next sibling parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Wednesday, 2 April 2014 at 04:59:11 UTC, Mike Parker wrote:
 People who are concerned about cross-language compatibility 
 should be implementing C interfaces to their C++ libraries 
 anyway. Yes, it's a nice, convenient feature to have, but 
 ultimately there are more important things, IMO.

I thought it was a stated goal of D to have C++ interoperability for the same compiler suite? E.g. dmd/dmc++, g++/gdc, ldc/clang…
Apr 02 2014
prev sibling parent Daniel =?ISO-8859-2?B?S2964Ws=?= <kozzi11 gmail.com> writes:
V Wed, 02 Apr 2014 10:57:15 +0000
"Ola Fosheim Grøstad"
<ola.fosheim.grostad+dlang gmail.com>" puremagic.com napsáno:

 On Wednesday, 2 April 2014 at 04:59:11 UTC, Mike Parker wrote:
 People who are concerned about cross-language compatibility 
 should be implementing C interfaces to their C++ libraries 
 anyway. Yes, it's a nice, convenient feature to have, but 
 ultimately there are more important things, IMO.

I thought it was a stated goal of D to have C++ interoperability for the same compiler suite? E.g. dmd/dmc++, g++/gdc, ldc/clang…

This would be awesome. But it would be much better if DMD has interoperability with g++ and/or clang too.
Apr 02 2014