Bind C++ class to DLang : undefined reference to `Canvas::Foo()'

evilrat evilrat666 at gmail.com
Tue Jul 14 01:09:40 UTC 2020


On Monday, 13 July 2020 at 09:34:35 UTC, zoujiaqing wrote:
> I changed string to basic_string.
>
> ///  source/main.d
> import std.stdio;
> import core.stdcpp.string;
>
> extern(C++)
> {
>     class Canvas
>     {
>         @disable this();
>
>         static Canvas Create();
>
>         basic_string!ubyte Foo();
>
>         basic_string!ubyte Bar();
>     };
> }
>
> void main()
> {
> 	Canvas canvas = Canvas.Create();
>
> 	writeln(canvas.Foo());
>
> 	writeln(canvas.Bar());
> }
>
> Error ...
>
> # dmd source/main.d Canvas.o -L-lstdc++ && ./main
> [1]    49078 segmentation fault  ./main

Putting std::string support aside there is another issue in this 
example. D has virtual by default semantic unlike C++.

One possible segfault reason is that because it tries to call non 
existing virtual method.

Marking those D methods 'final' should fix linking issues. Also I 
can't remember if extern(C++) is implicitly propagated to class 
methods so I would put extern(C++) in class body just to be sure.



More information about the Digitalmars-d-learn mailing list