Re: Interfacing with basic C++ class

2022-09-29 Thread Riccardo M via Digitalmars-d-learn
On Wednesday, 28 September 2022 at 20:41:13 UTC, Ali Çehreli 
wrote:

[...]

Ali


Thank you, that is perfect!

However that begs the following observation: it would be rather 
hard to link to a C++ library only by means of 'extern (C++)' if 
one should slightly rearrange C++ code as well. This sounds like 
it is rather obvious to the community, actually, but I am a 
recent addition to D language :)


Do you know that this is documented somewhere? The examples in 
the official docs are rather limited, maybe studying a github 
with previous work might help.


Cheers


Re: importC and cmake

2022-09-29 Thread Chris Piker via Digitalmars-d-learn

On Wednesday, 28 September 2022 at 06:04:36 UTC, zjh wrote:
On Wednesday, 28 September 2022 at 05:29:41 UTC, Chris Piker 
wrote:



`Xmake` is indeed simpler.



`Xmake` is really nice!


zjh

Sorry to go off topic for a moment, but do you happen to know how 
to tell xmake that my project is C only, and thus it shouldn't 
add the /TP flag to cl.exe?  The obvious statement:

```lua
set_languages("c99")
```
doesn't accomplish that task.

Thanks for the help,



Re: importC and cmake

2022-09-29 Thread zjh via Digitalmars-d-learn

On Thursday, 29 September 2022 at 20:56:50 UTC, Chris Piker wrote:


```lua
set_languages("c99")
```



Try:

`add_cxflags` or `add_files("src/*.c")` or
`set_languages("c")` or
`set_languages("c11")` .

or ,use `-l` to set language.

```cpp
xmake create -l c -t static test
```





Re: Interfacing with Rust

2022-09-29 Thread Ruby The Roobster via Digitalmars-d-learn

On Thursday, 29 September 2022 at 16:07:59 UTC, mw wrote:
On Thursday, 29 September 2022 at 16:02:43 UTC, Ruby The 
Roobster wrote:
Is there any way one can interface with Rust, such as with a 
struct, or a function?


I know that rust has an extern keyword, but I can't get it to 
work.


https://code.dlang.org/packages/rust_interop_d


Read the notes on memory management:

Only pass pointers as u64 as value type.


This isn't the issue.  I can't interface anything, period.


Re: Interfacing with basic C++ class

2022-09-29 Thread Ogi via Digitalmars-d-learn

On Wednesday, 28 September 2022 at 19:57:10 UTC, Riccardo M wrote:
I think I am stuck in the easiest of the issues and yet it 
seems I cannot get around this.

I have a C++ file:
```
class MyClass {
public:
int field;
MyClass(int a) : field(a) {}

int add(int asd) {
return asd + 1;
}
};
```


Ali is correct. However, you don’t have to change anything on the 
C++ side, just map C++ class to D struct:

```D
extern(C++, class) {
struct MyClass {
public: 
int field;
@disable this();
int add(int asd); //struct fields are always `final`
}

MyClass* instantiate(int asd); //mind the *
}
```


Re: Interfacing with basic C++ class

2022-09-29 Thread Ali Çehreli via Digitalmars-d-learn

On 9/29/22 01:28, Riccardo M wrote:

> if one should
> slightly rearrange C++ code as well.

Right. Additionally, the order of members must match (I am pretty sure, 
which means I am not :p).


> I am a recent addition to D
> language :)

Welcome! :)

> Do you know that this is documented somewhere? The examples in the
> official docs are rather limited

Manu Evans said at DConf 2022 that the documentation is very lacking 
compared to actual capability. People who know should update the docs.


Ali




Re: Interfacing with basic C++ class

2022-09-29 Thread Riccardo M via Digitalmars-d-learn

On Thursday, 29 September 2022 at 11:13:15 UTC, Ogi wrote:
Ali is correct. However, you don’t have to change anything on 
the C++ side, just map C++ class to D struct:

[...]


Thanks, this works perfectly.
Now i see that I can even instantiate directly from D, calling 
the default ctor, calling the custom ctor or even overriding the 
custom ctor in D (the fact that ctors could be called, in 
contrast with docs, was in fact hinted at DConf 2022 as suggested 
by Ali).


So it turns out that D's structs are a much better match for 
C++'s classes in this case. But why is this? Can you elaborate? 
It must have to do with the fact that D structs are passed by 
value?


Now this D code runs as expected:
```
// D
extern(C++, class) {
struct MyClass {
public: 
int field;
this(int a);
int add(int asd);
}

MyClass* instantiate(int asd);
}

void main()
{
import std : writeln;

	auto myclass = instantiate(100); //actually, instantiation 
through C++ is not required any longer

assert(myclass.field == 100);

auto myclass2 = new MyClass;
assert(myclass2.field == 0);

auto myclass3 = new MyClass(50);
assert(myclass3.field == 50);

assert(myclass3.add(40) == 90);
}
```
However the 'add' function only links correctly if C++ has the 
function body defined outside of its class.

```
// C++
int MyClass::add(int asd) {
return field + asd;
}
```
If the function is defined inside its class, it is an undefined 
reference at link time. Once again I ask for clarifications and 
workarounds, if possible.


Thanks


Interfacing with Rust

2022-09-29 Thread Ruby The Roobster via Digitalmars-d-learn
Is there any way one can interface with Rust, such as with a 
struct, or a function?


I know that rust has an extern keyword, but I can't get it to 
work.


Re: Interfacing with Rust

2022-09-29 Thread mw via Digitalmars-d-learn
On Thursday, 29 September 2022 at 16:02:43 UTC, Ruby The Roobster 
wrote:
Is there any way one can interface with Rust, such as with a 
struct, or a function?


I know that rust has an extern keyword, but I can't get it to 
work.


https://code.dlang.org/packages/rust_interop_d


Read the notes on memory management:

Only pass pointers as u64 as value type.