DPI:Fatal error while simulation C++ file

Hi,

fatal error comes in simulation when I tried .cpp file for DPI example,while same example successfully run with .c extension instead of .cpp
Here is the example I have tried in my Questasim 10.2a::

//----------------System Verilog File: abc.sv------------------------//
program main;
import “DPI-C” function void display();
initial
display();
endprogram
//-----------------------CPP File: xyz.cpp-------------------------//
#include “stdio.h”
#include “svdpi.h”
void display()
printf(“This is CPP Function”);
//------------------Compilation steps---------------------------//
vlog abc.sv xyz.cpp
vopt +acc main -o temp
vsim temp
//===================================================================
//========================Simulation Result==========================
Warning: Failed to find user defined “display” in C/C++ source files.
Fatal error:(abc.sv)Null foreign function pointer encountered when calling “display”

How to solve this error?

To link C++ code as C code, you need to add extern “C” in your cpp file. But a better thing to do when compiling any DPI code is to let the tool generate a C header file for you, then #include the file in you C or C++ file. That has an added benefit of checking all your argument types and return types at compile time, instead of crashing at run time.

//-----------------------CPP File: xyz.cpp-------------------------//
#include "stdio.h"
#include "svdpi.h"
#include "xyz.h"
void display()
printf("This is CPP Function");
//------------------Compilation steps---------------------------//
vlog abc.sv xyz.cpp -dpiheader xyz.h
vopt +acc main -o temp
vsim temp

In reply to dave_59:

Thanks a lot Dave…Its Done.