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

zoujiaqing zoujiaqing at gmail.com
Sun Jul 12 17:11:15 UTC 2020


/// 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());
}



# clang++ -I./include/ -fPIE -c src/Canvas.cpp
# dmd source/main.d Canvas.o -L-lstdc++ && ./main
/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





More information about the Digitalmars-d-learn mailing list