Jump to page: 1 2 3
Thread overview
Passing string from D to c++
Jul 30, 2013
Milvakili
Jul 30, 2013
Dicebot
Jul 30, 2013
Milvakili
Jul 30, 2013
John Colvin
Jul 30, 2013
Milvakili
Jul 30, 2013
Dicebot
Jul 31, 2013
monarch_dodra
Jul 31, 2013
deadalnix
Jul 31, 2013
monarch_dodra
Jul 30, 2013
John Colvin
Jul 31, 2013
Walter Bright
Jul 31, 2013
John Colvin
Jul 31, 2013
Dicebot
Jul 31, 2013
John Colvin
Jul 30, 2013
Adam D. Ruppe
Jul 30, 2013
Jesse Phillips
Jul 31, 2013
deadalnix
Jul 31, 2013
Jesse Phillips
Jul 31, 2013
deadalnix
Jul 31, 2013
Jesse Phillips
Jul 30, 2013
JS
Jul 30, 2013
bsd
Jul 30, 2013
bsd
Jul 31, 2013
Ali Çehreli
Jul 31, 2013
monarch_dodra
Jul 31, 2013
deadalnix
July 30, 2013
I'm linking D with C++ lib.a file.  When the C++ function has compatible data types I can call them from D.  But when I changed the parameter to string I got bunch of errors.

Data Type Compatibility table does not include strings.  Is there a way of passing strings?
July 30, 2013
On Tuesday, 30 July 2013 at 19:52:44 UTC, Milvakili wrote:
> I'm linking D with C++ lib.a file.  When the C++ function has compatible data types I can call them from D.  But when I changed the parameter to string I got bunch of errors.
>
> Data Type Compatibility table does not include strings.  Is there a way of passing strings?

http://dlang.org/phobos/std_string.html#.toStringz
July 30, 2013
On Tuesday, 30 July 2013 at 20:02:51 UTC, Dicebot wrote:
> On Tuesday, 30 July 2013 at 19:52:44 UTC, Milvakili wrote:
>> I'm linking D with C++ lib.a file.  When the C++ function has compatible data types I can call them from D.  But when I changed the parameter to string I got bunch of errors.
>>
>> Data Type Compatibility table does not include strings.  Is there a way of passing strings?
>
> http://dlang.org/phobos/std_string.html#.toStringz


So I need to pass them as char*, I can not pass them as string?
July 30, 2013
On Tuesday, 30 July 2013 at 20:09:01 UTC, Milvakili wrote:
> On Tuesday, 30 July 2013 at 20:02:51 UTC, Dicebot wrote:
>> On Tuesday, 30 July 2013 at 19:52:44 UTC, Milvakili wrote:
>>> I'm linking D with C++ lib.a file.  When the C++ function has compatible data types I can call them from D.  But when I changed the parameter to string I got bunch of errors.
>>>
>>> Data Type Compatibility table does not include strings.  Is there a way of passing strings?
>>
>> http://dlang.org/phobos/std_string.html#.toStringz
>
>
> So I need to pass them as char*, I can not pass them as string?

Correct.

Just thinking off the top of my head: you could probably hack something together though with a struct and casting.
July 30, 2013
On Tuesday, 30 July 2013 at 20:17:34 UTC, John Colvin wrote:
> On Tuesday, 30 July 2013 at 20:09:01 UTC, Milvakili wrote:
>> On Tuesday, 30 July 2013 at 20:02:51 UTC, Dicebot wrote:
>>> On Tuesday, 30 July 2013 at 19:52:44 UTC, Milvakili wrote:
>>>> I'm linking D with C++ lib.a file.  When the C++ function has compatible data types I can call them from D.  But when I changed the parameter to string I got bunch of errors.
>>>>
>>>> Data Type Compatibility table does not include strings.  Is there a way of passing strings?
>>>
>>> http://dlang.org/phobos/std_string.html#.toStringz
>>
>>
>> So I need to pass them as char*, I can not pass them as string?
>
> Correct.
>
> Just thinking off the top of my head: you could probably hack something together though with a struct and casting.

Thanks.  Is there any work in progress related to string passing?
I think string passing should be solved automatically.

Coz otherwise one need to write wrapper for each c++ function that has a string parameter.

thanks.
July 30, 2013
On Tuesday, 30 July 2013 at 20:09:01 UTC, Milvakili wrote:
> So I need to pass them as char*, I can not pass them as string?

I would pass it as a char* and a size_t for length.

extern(C++) void cpp_func(char* strptr, size_t strlen);


void main() {
   string foo = "test";
   cpp_func(foo.ptr, foo.length);
}


It'd be important on the C++ side to remember that it is NOT zero terminated, and always use that length parameter instead of normal C functions.

But that'd give the most efficiency, since that's using the same representation as D itself.
July 30, 2013
On Tuesday, 30 July 2013 at 20:22:46 UTC, Milvakili wrote:
> Thanks.  Is there any work in progress related to string passing?
> I think string passing should be solved automatically.
>
> Coz otherwise one need to write wrapper for each c++ function that has a string parameter.
>
> thanks.

c_function(toStringz("hello").ptr) does not seem that bad, what is the issue here?
July 30, 2013
On Tuesday, 30 July 2013 at 20:09:01 UTC, Milvakili wrote:

> So I need to pass them as char*, I can not pass them as string?

C++ doesn't have an immutable(char)[], it has char* and its own string class. To communicate with it one must choose a type both languages can understand, char* is that. D may be able to get away with providing a C++ string, but I'm not familiar with the integration layer.
July 30, 2013
On Tuesday, 30 July 2013 at 20:09:01 UTC, Milvakili wrote:
> On Tuesday, 30 July 2013 at 20:02:51 UTC, Dicebot wrote:
>> On Tuesday, 30 July 2013 at 19:52:44 UTC, Milvakili wrote:
>>> I'm linking D with C++ lib.a file.  When the C++ function has compatible data types I can call them from D.  But when I changed the parameter to string I got bunch of errors.
>>>
>>> Data Type Compatibility table does not include strings.  Is there a way of passing strings?
>>
>> http://dlang.org/phobos/std_string.html#.toStringz
>
>
> So I need to pass them as char*, I can not pass them as string?

You can't pass them as string because they have different representations. A C++ string is null terminated while a D string is not. If you pass a D string to C++ then it will not have the same length and give undesirable behavior.

If you really want to use strings, then wrap each C++ function with one that converts the string to a C string. If D used both pascal and C++ style strings as one(a waste of 1 extra byte per string) then life would be easier.


July 30, 2013
On Tuesday, 30 July 2013 at 20:22:46 UTC, Milvakili wrote:
> On Tuesday, 30 July 2013 at 20:17:34 UTC, John Colvin wrote:
>> On Tuesday, 30 July 2013 at 20:09:01 UTC, Milvakili wrote:
>>> On Tuesday, 30 July 2013 at 20:02:51 UTC, Dicebot wrote:
>>>> On Tuesday, 30 July 2013 at 19:52:44 UTC, Milvakili wrote:
>>>>> I'm linking D with C++ lib.a file.  When the C++ function has compatible data types I can call them from D.  But when I changed the parameter to string I got bunch of errors.
>>>>>
>>>>> Data Type Compatibility table does not include strings.  Is there a way of passing strings?
>>>>
>>>> http://dlang.org/phobos/std_string.html#.toStringz
>>>
>>>
>>> So I need to pass them as char*, I can not pass them as string?
>>
>> Correct.
>>
>> Just thinking off the top of my head: you could probably hack something together though with a struct and casting.
>
> Thanks.  Is there any work in progress related to string passing?
> I think string passing should be solved automatically.
>
> Coz otherwise one need to write wrapper for each c++ function that has a string parameter.
>
> thanks.

I had a crack at making this work, but I'm stuck on some basics and I don't know a great deal about c++ really

//inter.cpp
#include<string>
#include<iostream>
using namespace std;

void printString(string* s)
{
    cout << s << "\n";
}

string *strD2Cpp(char *ptr, unsigned long long length)
{
    return new std::string(ptr, length);
}

//inter.d
extern(C++)
{
    interface CppString{}
    CppString strD2Cpp(char* ptr, size_t length);
    void printString(CppString s);
}

void main()
{
    string s = "2432qreafdsa";
    auto s_cpp = strD2Cpp(s.dup.ptr, s.length);
    printString(s_cpp);
}

$ dmd -c -m64 inter.d
$ g++ -c -m64 inter.cpp -ointercpp.o
$ g++ inter.o intercpp.o -ointer -lphobos2
inter.o: In function `_Dmain':
inter.d:(.text._Dmain+0x48): undefined reference to `printString(CppString*)'
collect2: error: ld returned 1 exit status


Is this an incompatibility between dmd and g++? I'm on linux x64

How come it's happy to have CppString as the return type of strD2Cpp but not as a parameter to printString?

Also, using size_t in inter.cpp caused linker errors, hence the unsigned long long.
« First   ‹ Prev
1 2 3