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.