www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function with C calling convention but D mangling

reply Benjamin Thaut <code benjamin-thaut.de> writes:
Doing lots of C interfacing lately I wonder if there is a way to create 
a D function with C calling convention but D mangeling. I need this to 
place C callbacks onto functions inside D templates. Putting extern(C) 
before it does not work in this case. As extern(C) will also enforce C 
mangling I get lots of duplicate symbol errors from the linker. I know 
that I can work around this using mixin(string) but I wonder if there is 
a other solution to the problem.

Kind Regards
Benjamin Thaut
Dec 23 2013
parent reply "Dicebot" <public dicebot.lv> writes:
On Monday, 23 December 2013 at 10:57:09 UTC, Benjamin Thaut wrote:
 Doing lots of C interfacing lately I wonder if there is a way 
 to create a D function with C calling convention but D 
 mangeling. I need this to place C callbacks onto functions 
 inside D templates. Putting extern(C) before it does not work 
 in this case. As extern(C) will also enforce C mangling I get 
 lots of duplicate symbol errors from the linker. I know that I 
 can work around this using mixin(string) but I wonder if there 
 is a other solution to the problem.

 Kind Regards
 Benjamin Thaut
Does extern(C) + pragma(mangle) combo work?
Dec 23 2013
parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
Am 23.12.2013 11:59, schrieb Dicebot:
 On Monday, 23 December 2013 at 10:57:09 UTC, Benjamin Thaut wrote:
 Doing lots of C interfacing lately I wonder if there is a way to
 create a D function with C calling convention but D mangeling. I need
 this to place C callbacks onto functions inside D templates. Putting
 extern(C) before it does not work in this case. As extern(C) will also
 enforce C mangling I get lots of duplicate symbol errors from the
 linker. I know that I can work around this using mixin(string) but I
 wonder if there is a other solution to the problem.

 Kind Regards
 Benjamin Thaut
Does extern(C) + pragma(mangle) combo work?
Yes it would, but then I would have to define some mangling for myself.
Dec 23 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-12-23 12:20, Benjamin Thaut wrote:

 Yes it would, but then I would have to define some mangling for myself.
You can do something like this: void foo (); extern (C) pragma(mangle, foo.mangleof) void foo () { } I'm pretty sure you remove the duplication with a mixin. -- /Jacob Carlborg
Dec 27 2013