Compiling a mixed D and C++ project?

Jacob Carlborg doob at me.com
Tue Sep 3 08:57:56 UTC 2019


On 2019-09-02 22:29, Manu wrote:

> It looks like you're mixing up C++ classes and D structs, which are
> completely different.
> You shouldn't use extern(C), you should use extern(C++), and then
> you'll get proper errors when you fail to get the types correct,
> otherwise you'll continue to get surprising crashes like this when you
> match the types wrong.
> 
> My guess is that maybe the problem starts here:
> 
>      extern __gshared struct QtApplication;
>      QtApplication* TestApplication_Init();
> 
> QtApplication is a class with a vtable, so it should be `extern(C++)
> class` and definitely not `struct`.
> In D, classes are reference types, so you don't use '*' as in
> `QtApplication*` with classes, just `QtApplication`.

No, not necessarily. He has created C wrappers around the C++ code and 
is treating the C++ class as an opaque pointer. Have a look here [1].

> Also, that's just a weird line; what does it mean? I haven't seen an
> `extern __gshared` struct before... but I think you mean:
>    extern(C++) class QtApplication;
>    QtApplication TestApplication_Init();
> 

I think the actual problem is lines 32-35. The arguments to main are 
casted to a pointer to a D array, that won't work. It needs to be a C 
array, i.e. a pointer to a point of char. You can access the arguments 
to main as a C array from here [2].

[1] https://invent.kde.org/snippets/433
[2] https://dlang.org/phobos/core_runtime.html#.CArgs

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list