Calling std::string in C + + in the D language requires the use of the extern (c++,class) syntax, which is not supported in the DMD2.071 version and requires the use of ldc1.1.
: https://github.com/ldc-developers/ldc/releases/. Download Ldc2-1.1.0-alpha1-win32-msvc.zip
When using ldc2-1.1, you must use the VS2015 library file because you need to connect.
Use Visuald to test call std::string below.
Unpack the LDC in the following directory:
Use vs2015 to create a new project, such as:
To set up the LDC Path: (tools---options and projects and SOLUTIONS)
Settings using the LDC Compiler: (project attribute-->general)
Compiling a 32-bit program with LDC does not require setting the Ms-coff format, because he is using the Ms-coff format.
one, the test string* call (completely Normal)
Test.cpp
#include <stdio.h>#include<iostream>#include<string>using namespacestd;extern "C + +"{ string*getstdstring () {return New string("Hello std::string"); } voidPrintstdstring (string*Str) {printf ("str:\r\n"); printf ("%s\r\n",str->C_str ()); }}
Std.d
Module stl;extern(c + +, Std) { extern(c + +,class)structAllocator (CharT); structchar_traits (CharT) {}extern(c + +,class)structbasic_string (CharT, chartraits = char_traits! ( CharT), Allocator = allocator!(CharT)) {}}extern(c + +) {alias String= std.basic_string! (Char); voidPrintstdstring (string*str); String*getstdstring ();}voidtest () {auto str=getstdstring (); Printstdstring (str);}
Main.d
Import Std.stdio;import stl; int Main (string[] argv) { test (); READLN (); return 0 ;}
second, the test string& call (the result is not normal)
Test.cpp
#include <stdio.h>#include<iostream>#include<string>using namespacestd;extern "C + +"{ string&getstdstring () {return string("Hello std::string"); } voidPrintstdstring (string&Str) {printf ("str:\r\n"); printf ("%s\r\n", Str.c_str ()); }}
Stl.d
Module stl;extern(c + +, Std) { extern(c + +,class)structAllocator (CharT); structchar_traits (CharT) {}extern(c + +,class)structbasic_string (CharT, chartraits = char_traits! ( CharT), Allocator = allocator!(CharT)) {}}extern(c + +) {alias String= std.basic_string! (Char); voidPrintstdstring (refString str); refString getstdstring ();}voidtest () {auto str=getstdstring (); Printstdstring (str);}
Main.d
Import Std.stdio;import stl; int Main (string[] argv) { test (); READLN (); return 0 ;}
Compile Run:
Can compile through, also can run, just not want the structure, specific problems also need analysis ...
Iii. call to test the C_str () method of the string* (fully Normal)
Call the C_str () method of the string
Test.cpp
#include <stdio.h>#include<iostream>#include<string>using namespacestd;extern "C + +"{ string*getstdstring () {return New string("Hello std::string"); } voidPrintstdstring (string*Str) {printf ("C + + output:%s\r\n",str->C_str ()); }}
Stl.d
Module stl;extern(c + +, Std) { extern(c + +,class)structAllocator (CharT); structchar_traits (CharT) {}extern(c + +,class)structbasic_string (CharT, chartraits = char_traits! ( CharT), Allocator = allocator!(CharT)) { extern(c + +) public Const Const(CharT) *C_str (); }}extern(c + +) {alias String= std.basic_string! (Char); voidPrintstdstring (string*str); String*getstdstring ();}
Main.d
import std.stdio;import stl;import std. string ; void Test () { = getstdstring (); = Fromstringz (str.c_str ()); Printstdstring (str); Writeln ("D ", dstr);} int Main (string[] argv) { test (); READLN (); return 0 ;}
Compile Run:
Finish running Successfully.
D language calls std::string in C + +