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
}
Syntax

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.

main.cpp
display.d
import std.stdio;
// Providing access to c++ program
extern (C++) void display()
{
writefln("I am from D function");
}
// Providing access to func function from c++ program
extern (C++) void func();
void main()
{
func();
}

Explanation

  • Line 3: We use extern(C++)void display() to provide access to the display function from the D program file.

  • Line 8: We give access to the display function to the func function from C++ by using extern (C++) void func(); .

In the main.cpp file:

  • Line 5: We simply call the display function from the D program file.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved