Linking C++ with a D-library

Milvakili maliy.kayit at gmail.com
Wed Jun 26 13:32:55 PDT 2013


On Wednesday, 26 June 2013 at 20:23:35 UTC, Adam D. Ruppe wrote:
> On Wednesday, 26 June 2013 at 20:19:07 UTC, Milvakili wrote:
>> Hi, I'm a new to D and I could not find any relevant answers 
>> in the forum.
>>
>> I want to call functions from D-lib from c++ main.
>> dlibrary
>>
>> import std.stdio;
>>
>> extern (C++) void foo(int i, int j, int k) {
>>  writefln("i = %s", i);
>>  writefln("j = %s", j);
>>  writefln("k = %s", k);
>> }
>> void main(){}
>
>
> Try adding this to D:
>
> extern(C) void initialize_D() {
>   import core.runtime;
>   Runtime.initialize();
> }
>
>
>
> And this to C++:
>
>> void foo(int i, int j, int k);
>
> extern "C" void initialize_d();
>
>> using namespace std;
>>
>> int main(){
>
>     initialize_d();
>
>>  cout << "This is the main of C++\n";
>>  foo(1,3,4);
>>  return 0;
>> }
>
>
> And you should see something better.

That "magically" solves my problem.  Would you mind if I ask you 
to define what this initialization is all about or guide me to 
some tutorial related to these issues?

Thanks alot.


More information about the Digitalmars-d mailing list