How to call global D functions from C++
In D, we have great interfacing with the C++ language that provides excellent integration between both languages, which helps the programmer use the D program's functions in the C++ program. Similarly, from the D program, we can use the C++ program.
In this Answer, we will learn how to call the D global function from our C++ program file.
Syntax
Let's look into the syntax to understand how it works:
extern (C++) void display()//declaring a function{//Define function}
Code example
In the D program language, we use extern(C++) to get access to this function from the C++ program. After using it, we can declare our function.
import std.stdio;// Providing access to c++ programextern (C++) void display(){writefln("I am from D function");}// Providing access to func function from c++ programextern (C++) void func();void main(){func();}
Explanation
Line 3: We use
extern(C++)void display()to provide access to thedisplayfunction from the D program file.Line 8: We give access to the
displayfunction to thefuncfunction from C++ by usingextern (C++) void func();.
In the main.cpp file:
Line 5: We simply call the
displayfunction from the D program file.
Free Resources