digitalmars.D.learn - Bind C++ class to DLang : undefined reference to `Canvas::Foo()'
- zoujiaqing (49/49) Jul 12 2020 /// Canvas.hpp
- zoujiaqing (23/23) Jul 13 2020 I changed string to basic_string.
- Adam D. Ruppe (5/7) Jul 13 2020 On my computer I got this warning out of the compiler:
- zoujiaqing (2/10) Jul 13 2020 Thanks Adam, but D link to C++ very hard?
- Boris Carvajal (6/20) Jul 13 2020 Can you try passing -D_GLIBCXX_USE_CXX11_ABI=0 to g++ and
- Jacob Carlborg (5/11) Jul 15 2020 I based on previous messages and the usage of Clang, I think zoujiaqing
- evilrat (8/31) Jul 13 2020 Putting std::string support aside there is another issue in this
/// Canvas.hpp #include <iostream>; class Canvas { static Canvas* Create(); std::string Foo(); std::string Bar(); }; /// Canvas.cpp #include "Canvas.hpp" #include <iostream> Canvas* Canvas::Create() { return new Canvas(); } std::string Canvas::Foo() { return "Foo"; } std::string Canvas::Bar() { return "Bar"; } /// main.d import std.stdio; extern(C++) { class Canvas { disable this(); static Canvas Create(); string Foo(); string Bar(); }; } void main() { Canvas canvas = Canvas.Create(); writeln(canvas.Foo()); writeln(canvas.Bar()); } /usr/bin/ld: main.o:(.data._D4main6Canvas6__vtblZ+0x0): undefined reference to `Canvas::Foo()' /usr/bin/ld: main.o:(.data._D4main6Canvas6__vtblZ+0x8): undefined reference to `Canvas::Bar()' collect2: error: ld returned 1 exit status Error: linker exited with status 1
Jul 12 2020
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 ... [1] 49078 segmentation fault ./main
Jul 13 2020
On Monday, 13 July 2020 at 09:34:35 UTC, zoujiaqing wrote:[1] 49078 segmentation fault ./mainOn my computer I got this warning out of the compiler: libstdc++ std::__cxx11::basic_string is not yet supported; the struct contains an interior pointer which breaks D move semantics! sounds like it might be a known limitation.
Jul 13 2020
On Monday, 13 July 2020 at 12:57:52 UTC, Adam D. Ruppe wrote:On Monday, 13 July 2020 at 09:34:35 UTC, zoujiaqing wrote:Thanks Adam, but D link to C++ very hard?[1] 49078 segmentation fault ./mainOn my computer I got this warning out of the compiler: libstdc++ std::__cxx11::basic_string is not yet supported; the struct contains an interior pointer which breaks D move semantics! sounds like it might be a known limitation.
Jul 13 2020
On Monday, 13 July 2020 at 16:17:55 UTC, zoujiaqing wrote:On Monday, 13 July 2020 at 12:57:52 UTC, Adam D. Ruppe wrote:Can you try passing -D_GLIBCXX_USE_CXX11_ABI=0 to g++ and -version=_GLIBCXX_USE_CXX98_ABI to dmd. That comes from: https://dlang.org/changelog/2.088.0.html#std_string C++11 ABI is currently not supported.On Monday, 13 July 2020 at 09:34:35 UTC, zoujiaqing wrote:Thanks Adam, but D link to C++ very hard?[1] 49078 segmentation fault ./mainOn my computer I got this warning out of the compiler: libstdc++ std::__cxx11::basic_string is not yet supported; the struct contains an interior pointer which breaks D move semantics! sounds like it might be a known limitation.
Jul 13 2020
On 2020-07-14 05:33, Boris Carvajal wrote:Can you try passing -D_GLIBCXX_USE_CXX11_ABI=0 to g++ and -version=_GLIBCXX_USE_CXX98_ABI to dmd. That comes from: https://dlang.org/changelog/2.088.0.html#std_string C++11 ABI is currently not supported.I based on previous messages and the usage of Clang, I think zoujiaqing is using a Mac. On Mac libc++ is used. That above might not apply. -- /Jacob Carlborg
Jul 15 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 ... [1] 49078 segmentation fault ./mainPutting 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.
Jul 13 2020